-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add menu trigger component (#135)
- Loading branch information
1 parent
da7bdf3
commit 000b99e
Showing
4 changed files
with
55 additions
and
0 deletions.
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,52 @@ | ||
'use client' | ||
|
||
import type * as Polymophic from '@/utilities/polymorphic' | ||
import type { ButtonProps } from 'react-aria-components' | ||
|
||
import React from 'react' | ||
import { Button } from 'react-aria-components' | ||
|
||
import { useMenuContext } from '@/components/menu/use-menu.hook' | ||
|
||
const __ELEMENT_TYPE__ = 'button' | ||
|
||
type ComponentOwnProps = ButtonProps | ||
|
||
type ComponentProps<TElement extends React.ElementType = typeof __ELEMENT_TYPE__> = Polymophic.ComponentPropsWithRef< | ||
TElement, | ||
ComponentOwnProps | ||
> | ||
|
||
type ComponentType = <TElement extends React.ElementType = typeof __ELEMENT_TYPE__>( | ||
props: ComponentProps<TElement> | ||
) => React.ReactNode | ||
|
||
const Component: ComponentType = React.forwardRef( | ||
<TElement extends React.ElementType = typeof __ELEMENT_TYPE__>( | ||
props: ComponentProps<TElement>, | ||
ref: Polymophic.Ref<TElement> | ||
) => { | ||
const { as, children, className, ...rest } = props | ||
|
||
const Element = as ?? Button | ||
|
||
const { slots } = useMenuContext() | ||
|
||
const component = React.useMemo<ButtonProps>( | ||
() => ({ | ||
className: slots.trigger({ className: className?.toString() }), | ||
...rest, | ||
}), | ||
[className, rest, slots] | ||
) | ||
|
||
return ( | ||
<Element {...component} ref={ref}> | ||
{children} | ||
</Element> | ||
) | ||
} | ||
) | ||
|
||
export type { ComponentOwnProps as MenuTriggerOwnProps, ComponentProps as MenuTriggerProps } | ||
export default Component |
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