Skip to content

Commit

Permalink
feat(Dropdown): add support for item disabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitano committed Aug 3, 2023
1 parent e0c2846 commit 62ad043
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
67 changes: 58 additions & 9 deletions packages/dropdown/src/Dropdown.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import type {DropdownItemProps} from './types';
import Button from '@morpheme/button';
import DropdownBtn from './DropdownBtn.vue';

const genItems = (length = 5): DropdownItemProps[] =>
Array.from({length}, (_, v) => ({
text: `Item ${v + 1}`,
icon: 'ri:home-line',
}));

const items = [...genItems(2), {divider: true}, ...genItems(3)];
const items = [
{
text: 'View',
icon: 'ic:round-visibility',
},
{
text: 'Edit',
icon: 'ic:round-edit',
},
{
divider: true,
},
{
text: 'Delete',
icon: 'ic:round-delete',
},
];

export default {
title: 'Components/Dropdown',
Expand Down Expand Up @@ -54,6 +64,41 @@ PrefixIcon.args = {
label: 'Account',
};

export const Disabled = Template.bind({});
Disabled.args = {
items: [
{
text: 'Default',
},
{
text: 'Disabled',
disabled: true,
},
{
text: 'RouterLink',
to: '/home',
disabled: true,
},
{
text: 'NuxtLink',
to: '/nuxt',
nuxt: true,
disabled: true,
},
{
text: 'Go to Google',
href: 'https://google.com',
disabled: true,
},
{
text: 'Go to Google (New Tab)',
href: 'https://google.com',
disabled: true,
newTab: true,
},
],
};

export const RouterLink = Template.bind({});
RouterLink.args = {
items: [
Expand Down Expand Up @@ -150,13 +195,17 @@ export const CustomActivator: Story = (args, {argTypes}) => ({
});

export const DarkMode: Story<{}> = (args) => ({
components: {Dropdown},
components: {Dropdown, DropdownItem},
setup() {
return {args};
},
template: `
<main class="dark dark:bg-neutral-900 dark:text-neutral-200 p-6 space-y-2">
<Dropdown v-bind="args"/>
<Dropdown v-bind="args">
<template #append>
<DropdownItem disabled>Disabled</DropdownItem>
</template>
</Dropdown>
</main>
`,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/dropdown/src/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ defineSlots<{
default?: (props: {}) => any;
activator?: (props: {btnProps: Record<string, any>; label: string}) => any;
item?: (props: DropdownItemProps) => any;
prepend?: (props: {}) => any;
append?: (props: {}) => any;
}>();
</script>

Expand Down Expand Up @@ -118,9 +120,11 @@ defineSlots<{
panelClass,
]"
>
<slot name="prepend" />
<slot>
<DropdownItem v-for="item in items" :key="item.text" v-bind="item" />
</slot>
<slot name="append" />
</MenuItems>
</Float>
</Menu>
Expand Down
17 changes: 4 additions & 13 deletions packages/dropdown/src/DropdownItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ const attributes = computed(() => {
}
if (props.href) {
attrs['href'] = props.href;
attrs['href'] = props.disabled ? 'javascript:void(0)' : props.href;
}
if (props.newTab) {
if (props.newTab && !props.disabled) {
attrs['rel'] = 'noopener';
attrs['target'] = '_blank';
}
Expand All @@ -57,24 +57,14 @@ const attributes = computed(() => {
};
});
// const attributes = computed(() => {
// return {
// to: props.to ?? undefined,
// href: props.href ?? undefined,
// target: props.href && props.newTab ? '_blank' : undefined,
// rel: props.href && props.newTab ? 'noopener' : undefined,
// ...useAttrs(),
// };
// });
defineSlots<{
default?: (props: {}) => any;
icon?: (props: {}) => any;
}>();
</script>

<template>
<MenuItem v-slot="{active}">
<MenuItem v-slot="{active}" :disabled="disabled">
<div v-if="divider" class="dropdown-divider"></div>
<component
v-else
Expand All @@ -83,6 +73,7 @@ defineSlots<{
'group dropdown-item',
{
'dropdown-item--active': active,
'dropdown-item--disabled': disabled,
},
]"
v-bind="attributes"
Expand Down
3 changes: 3 additions & 0 deletions packages/themes/src/morpheme/_dropdown.dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
// button activator
--dropdown-button-activator-border-color: var(--color-gray-true-700);
--dropdown-button-activator-color: var(--color-gray-true-300);

// disabled
--dropdown-item-disabled-color: var(--color-gray-true-600);
}
}
12 changes: 11 additions & 1 deletion packages/themes/src/morpheme/_dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
--dropdown-button-activator-font-size: var(--size-font-sm);
--dropdown-button-activator-font-weight: var(--font-weight-semibold);
--dropdown-button-activator-line-height: 20px;

// disabled
--dropdown-item-disabled-color: var(--color-gray-400);
}

.dropdown {
Expand Down Expand Up @@ -121,7 +124,7 @@
font-weight: var(--dropdown-item-font-weight);
line-height: var(--dropdown-item-line-height);

&--active {
&--active:not(:disabled) {
--dropdown-item-color: var(--dropdown-item-active-color);
--dropdown-item-bg-color: var(--dropdown-item-active-bg-color);
}
Expand All @@ -131,6 +134,13 @@
height: var(--dropdown-item-icon-height) !important;
color: var(--dropdown-item-icon-color);
}

&--disabled,
:disabled,
[aria-disabled='true'] {
cursor: not-allowed;
--dropdown-item-color: var(--dropdown-item-disabled-color);
}
}

&-button-icon {
Expand Down

0 comments on commit 62ad043

Please sign in to comment.