-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Menu } from '@headlessui/react'; | ||
import clsx from 'clsx'; | ||
import React, { ReactNode } from 'react'; | ||
|
||
export interface DropdownProps { | ||
/** | ||
* The color of the button, only works when the variant is `color` or `hover` | ||
*/ | ||
color?: 'red' | 'orange' | 'amber' | 'yellow' | 'lime' | 'green' | 'teal' | 'cyan' | 'blue' | 'indigo' | 'violet' | 'purple' | 'pink' | 'rose' | 'brown' | 'grey' | 'accent'; | ||
/** | ||
* Value of the textarea | ||
*/ | ||
trigger: ReactNode; | ||
/** | ||
* Custom classes for the label | ||
*/ | ||
className?: string; | ||
/** | ||
* Custom classes for the label | ||
*/ | ||
children?: ReactNode; | ||
} | ||
|
||
/** | ||
* Primary UI component for user interaction | ||
*/ | ||
export const Dropdown = ({ | ||
className, | ||
color, | ||
trigger, | ||
children, | ||
...props | ||
}: DropdownProps) => { | ||
return ( | ||
<div className="dropdown"> | ||
<Menu> | ||
<Menu.Button as={React.Fragment}>{trigger}</Menu.Button> | ||
<Menu.Items | ||
className={clsx( | ||
'dropdown-menu', | ||
color, | ||
className | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
</Menu.Items> | ||
</Menu> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Dropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './Dropdown'; | ||
export type { DropdownProps } from './Dropdown'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Menu } from '@headlessui/react'; | ||
import clsx from 'clsx'; | ||
import React, { ElementType, ReactNode, useMemo } from 'react'; | ||
|
||
export interface DropdownItemProps<T> { | ||
/** | ||
* The color of the button, only works when the variant is `color` or `hover` | ||
*/ | ||
color?: 'red' | 'orange' | 'amber' | 'yellow' | 'lime' | 'green' | 'teal' | 'cyan' | 'blue' | 'indigo' | 'violet' | 'purple' | 'pink' | 'rose' | 'brown' | 'grey' | 'accent'; | ||
/** | ||
* Button disabled state. | ||
*/ | ||
disabled?: boolean; | ||
/** | ||
* If we have a href, it's an anchor. | ||
*/ | ||
href?: string; | ||
/** | ||
* Custom classes for the label | ||
*/ | ||
className?: string; | ||
/** | ||
* As what the component should be rendered | ||
*/ | ||
as?: T; | ||
/** | ||
* Custom classes for the label | ||
*/ | ||
children?: ReactNode; | ||
} | ||
|
||
/** | ||
* Primary UI component for user interaction | ||
*/ | ||
export function Dropdown<T>({ | ||
href, | ||
className, | ||
as, | ||
color, | ||
disabled = false, | ||
children, | ||
...props | ||
}: DropdownItemProps<T>) { | ||
const Component = useMemo(() => (as ? as : (href ? 'a' : 'button')) as ElementType, [as, href]); | ||
|
||
return ( | ||
<Menu.Item> | ||
{({ active }) => ( | ||
<Component | ||
type="button" | ||
className={clsx( | ||
'dropdown-item', | ||
{ | ||
'active': active, | ||
}, | ||
color, | ||
className | ||
)} | ||
href={href} | ||
disabled={disabled} | ||
{...props} | ||
> | ||
{children} | ||
</Component> | ||
)} | ||
</Menu.Item> | ||
); | ||
}; | ||
|
||
export default Dropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './DropdownItem'; | ||
export type { DropdownItemProps } from './DropdownItem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useState } from "react"; | ||
|
||
import DemoBox from "@docs/components/DemoBox"; | ||
|
||
import { | ||
Button, | ||
Dropdown, | ||
DropdownItem | ||
} from "@components"; | ||
|
||
export default function GridDemo() { | ||
const [open, setOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<h2>Dialog</h2> | ||
<DemoBox> | ||
<Dropdown | ||
trigger={ | ||
<Button>Open dropdown</Button> | ||
} | ||
> | ||
<DropdownItem>Hello</DropdownItem> | ||
</Dropdown> | ||
</DemoBox> | ||
<h3>Color</h3> | ||
<DemoBox> | ||
<Dropdown | ||
color="indigo" | ||
trigger={ | ||
<Button>Open dropdown</Button> | ||
} | ||
> | ||
<DropdownItem>Hello</DropdownItem> | ||
<DropdownItem>World</DropdownItem> | ||
<DropdownItem color="red">Delete</DropdownItem> | ||
</Dropdown> | ||
</DemoBox> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters