Skip to content

Commit

Permalink
fix(Dropdown): refactored Item
Browse files Browse the repository at this point in the history
fix(Dropdown): added Divider
docs(dropdown): updated examples
  • Loading branch information
N00nDay committed Oct 21, 2022
1 parent 8848f45 commit 677a33a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 21 deletions.
22 changes: 22 additions & 0 deletions src/lib/components/dropdown/Divider.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import { twMerge } from 'tailwind-merge';
import { get_current_component } from 'svelte/internal';
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions';
export let use: ActionArray = [];
import { exclude } from '../../utils/exclude';
const forwardEvents = forwardEventsBuilder(get_current_component());
const defaultClass = 'relative my-2 h-px';
$: finalClass = twMerge(defaultClass, $$props.class);
</script>

<div
class={finalClass}
use:useActions={use}
use:forwardEvents
{...exclude($$props, ['use', 'class'])}
>
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t light-border-base dark:dark-border-base" />
</div>
</div>
13 changes: 11 additions & 2 deletions src/lib/components/dropdown/Item.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { DROPDOWN_CONTEXT_ID } from './Dropdown.svelte';
import HoverBackground from '../HoverBackground.svelte';
import { useContext } from '../../utils/useContext';
import { twMerge } from 'tailwind-merge';
import { get_current_component } from 'svelte/internal';
Expand All @@ -8,14 +9,16 @@
import { exclude } from '../../utils/exclude';
const forwardEvents = forwardEventsBuilder(get_current_component());
export let label: string;
useContext({
context_id: DROPDOWN_CONTEXT_ID,
parent: 'Dropdown',
component: 'DropdownItem'
});
const defaultClass =
'w-full group flex items-center px-3 py-2 text-sm font-medium rounded-md overflow-hidden text-light-secondary-content dark:text-dark-secondary-content dark:hover:bg-dark-icon-background-hover hover:bg-light-icon-background-hover hover:text-light-content dark:hover:text-dark-content';
'w-full group relative flex items-center px-3 py-2 text-sm font-medium rounded-md overflow-hidden text-light-secondary-content dark:text-dark-secondary-content';
$: finalClass = twMerge(defaultClass, $$props.class);
</script>

Expand All @@ -26,5 +29,11 @@
use:forwardEvents
{...exclude($$props, ['use', 'class'])}
>
<slot />
<span class="flex items-center justify-start flex-grow">
<slot name="icon" />
<span class="truncate" class:ml-3={$$slots.icon}>{label}</span>
</span>

<slot name="extra" />
<HoverBackground class="rounded-md" />
</button>
14 changes: 12 additions & 2 deletions src/lib/components/dropdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import OriginalDropdown from './Dropdown.svelte';
import OriginalItems from './Items.svelte';
import Item from './Item.svelte';
import Divider from './Divider.svelte';
import Icon from '../icon/Icon.svelte';

const Dropdown = OriginalDropdown as DropdownStatic;
Dropdown.Items = OriginalItems as DropdownItemsStatic;
Dropdown.Items.Item = Item;
Dropdown.Items.Item = Item as ItemStatic;
Dropdown.Items.Divider = Divider;
Dropdown.Items.Item.Icon = Icon;

export default Dropdown;

Expand All @@ -15,5 +19,11 @@ export interface DropdownStatic {

export interface DropdownItemsStatic {
new (...args: ConstructorParameters<typeof OriginalItems>): OriginalItems;
Item: typeof Item;
Item: ItemStatic;
Divider: typeof Divider;
}

export interface ItemStatic {
new (...args: ConstructorParameters<typeof Item>): Item;
Icon: typeof Icon;
}
46 changes: 38 additions & 8 deletions src/routes/dropdown/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<script lang="ts">
import { Dropdown, Button, Card, Col } from '../../lib';
import { example, props, slots, itemsProps, itemsSlots, itemSlots } from './examples';
import { Dropdown, Button, Card, Col, Badge } from '../../lib';
import {
example,
props,
slots,
itemsProps,
itemsSlots,
itemSlots,
itemProps,
iconProps
} from './examples';
import { PropsTable, SlotsTable, CodeBlock } from '../../docs';
import { home, trash } from '../../docs/icons';
let visible1 = false;
let visible2 = false;
Expand Down Expand Up @@ -32,9 +42,21 @@
<Dropdown bind:visible={visible1}>
<Button slot="trigger" type="primary" on:click={toggleDropdown1}>Toggle Dropdown</Button>
<Dropdown.Items slot="items">
<Dropdown.Items.Item on:click={closeDropdown1}>Item 1</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1}>Item 2</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1}>Item 3</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1} label="Item 1">
<Dropdown.Items.Item.Icon slot="icon" data={trash} />
</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1} label="Item 2">
<Dropdown.Items.Item.Icon slot="extra" data={trash} />
</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1} label="Notifications">
<Badge type="info" slot="extra">+99</Badge>
</Dropdown.Items.Item>
<Dropdown.Items.Divider />
<Button type="danger" class="w-full justify-between">
<Button.Leading slot="leading" data={home} />
Home
<Button.Trailing slot="trailing" data={home} />
</Button>
</Dropdown.Items>
</Dropdown>

Expand All @@ -55,9 +77,9 @@
/>
</button>
<Dropdown.Items slot="items" placement="right">
<Dropdown.Items.Item on:click={closeDropdown2}>Item 1</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown2}>Item 2</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown2}>Item 3</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown2} label="Item 1" />
<Dropdown.Items.Item on:click={closeDropdown2} label="Item 2" />
<Dropdown.Items.Item on:click={closeDropdown2} label="Item 3" />
</Dropdown.Items>
</Dropdown>
</div>
Expand All @@ -84,6 +106,14 @@
<SlotsTable component="Dropdown.Items" slots={itemsSlots} />
</Col>

<Col class="col-24">
<PropsTable component="Dropdown.Items.Item" props={itemProps} />
</Col>

<Col class="col-24">
<SlotsTable component="Dropdown.Items.Item" slots={itemSlots} />
</Col>

<Col class="col-24">
<PropsTable component="Dropdown.Items.Item.Icon" props={iconProps} />
</Col>
95 changes: 86 additions & 9 deletions src/routes/dropdown/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,82 @@ export const itemsSlots: Slot[] = [
}
];

export const itemProps: Prop[] = [
{
id: '1',
prop: 'label',
type: 'string',
default: ''
}
];

export const itemSlots: Slot[] = [
{
id: '1',
slot: 'default',
slot: 'icon',
component: '<Dropdown.Items.Item.Icon slot="icon" />'
},
{
id: '2',
slot: 'extra',
component: ''
}
];

export const iconProps: Prop[] = [
{
id: '1',
prop: 'data',
type: '<a href="/types#IconData" class="link">string (IconData)</a>',
default: ''
},
{
id: '2',
prop: 'viewBox',
type: 'string',
default: '0 0 24 24'
},
{
id: '3',
prop: 'size',
type: 'string',
default: '24px'
},
{
id: '4',
prop: 'width',
type: 'string',
default: '24px'
},
{
id: '5',
prop: 'height',
type: 'string',
default: '24px'
},
{
id: '6',
prop: 'color',
type: 'string',
default: 'currentColor'
},
{
id: '7',
prop: 'stroke',
type: 'string | undefined',
default: ''
},
{
id: '8',
prop: 'fill',
type: 'string',
default: 'currentColor'
}
];

export const example = `
<script lang="ts">
import { Dropdown } from 'stwui';
import { Dropdown, Button, Badge } from 'stwui';
let visible = false;
Expand All @@ -63,11 +128,23 @@ export const example = `
}
</script>
<Dropdown {visible}>
<Button slot="trigger" type="primary" on:click={toggleDropdown}>Toggle Dropdown</Button>
<Dropdown.Items slot="items">
<Dropdown.Items.Item on:click={closeDropdown}>Item 1</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown}>Item 2</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown}>Item 3</Dropdown.Items.Item>
</Dropdown.Items>
<Dropdown bind:visible={visible1}>
<Button slot="trigger" type="primary" on:click={toggleDropdown1}>Toggle Dropdown</Button>
<Dropdown.Items slot="items">
<Dropdown.Items.Item on:click={closeDropdown1} label="Item 1">
<Dropdown.Items.Item.Icon slot="icon" data={trash} />
</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1} label="Item 2">
<Dropdown.Items.Item.Icon slot="extra" data={trash} />
</Dropdown.Items.Item>
<Dropdown.Items.Item on:click={closeDropdown1} label="Notifications">
<Badge type="info" slot="extra">+99</Badge>
</Dropdown.Items.Item>
<Dropdown.Items.Divider />
<Button type="danger" class="w-full justify-between">
<Button.Leading slot="leading" data={home} />
Home
<Button.Trailing slot="trailing" data={home} />
</Button>
</Dropdown.Items>
</Dropdown>`;

1 comment on commit 677a33a

@vercel
Copy link

@vercel vercel bot commented on 677a33a Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

stwui – ./

stwui-git-main-n00nday.vercel.app
stwui.vercel.app
stwui-n00nday.vercel.app

Please sign in to comment.