-
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 polymorphic component support (#21)
* feat: add polymorphic types * feat(button): add polymorphic component support * feat(alert): add polymorphic component support * feat(link): add polymorphic component support * feat(alert): use Heading and Text base components * feat(avatar): add polymorphic component support * feat(table): add polymorphic component support * feat(typography): add polymorphic component support * feat(navigation): add polymorphic component support * feat(switch): add polymorphic component support * feat(select): add polymorphic component support * feat(progress): add polymorphic component support * feat(menu): add polymorphic component support * feat(input): add polymorphic component support * feat(form): add polymorphic component support * feat(divider): add polymorphic component support * feat(dialog): add polymorphic component support * feat(chip): add polymorphic component support * feat(checkbox): add polymorphic component support * feat(card): add polymorphic component support * feat(breadcrumb): add polymorphic component support * fix(button): base button props missing
- Loading branch information
1 parent
a5e3a10
commit 74bf50b
Showing
85 changed files
with
2,255 additions
and
1,580 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
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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
import type { Component } from '@/utilities/types' | ||
import type * as Polymophic from '@/utilities/polymorphic' | ||
|
||
import React from 'react' | ||
|
||
import { useAlertContext } from '@/components/alert/use-alert.hook' | ||
|
||
export type AlertBodyProps = Component<'div'> | ||
const __ELEMENT_TYPE__ = 'div' | ||
|
||
const AlertBody = React.forwardRef<HTMLDivElement, AlertBodyProps>((props, ref) => { | ||
const { as, children, className, ...rest } = props | ||
const { slots } = useAlertContext() | ||
type ComponentOwnProps = {} | ||
|
||
const Component = as || 'div' | ||
type ComponentProps<T extends React.ElementType> = Polymophic.ComponentPropsWithRef<T, ComponentOwnProps> | ||
|
||
const getProps = React.useCallback( | ||
() => ({ | ||
ref, | ||
className: slots.body({ | ||
class: className, | ||
}), | ||
...rest, | ||
}), | ||
[ref, slots, className, rest] | ||
) | ||
type ComponentType = <T extends React.ElementType = typeof __ELEMENT_TYPE__>( | ||
props: ComponentProps<T> | ||
) => React.ReactNode | ||
|
||
const Component: ComponentType = React.forwardRef( | ||
<T extends React.ElementType = typeof __ELEMENT_TYPE__>(props: ComponentProps<T>, ref: Polymophic.Ref<T>) => { | ||
const { as, children, className, ...rest } = props | ||
|
||
return <Component {...getProps()}>{children}</Component> | ||
}) | ||
const Element = as ?? __ELEMENT_TYPE__ | ||
|
||
AlertBody.displayName = 'Alert.Body' | ||
const { slots } = useAlertContext() | ||
|
||
export default AlertBody | ||
const component = React.useMemo<React.ComponentPropsWithoutRef<typeof __ELEMENT_TYPE__>>( | ||
() => ({ | ||
className: slots.body({ className }), | ||
...rest, | ||
}), | ||
[className, rest, slots] | ||
) | ||
|
||
return ( | ||
<Element {...component} ref={ref}> | ||
{children} | ||
</Element> | ||
) | ||
} | ||
) | ||
|
||
export type { ComponentOwnProps as AlertBodyProps } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
import type { Component } from '@/utilities/types' | ||
import type * as Polymophic from '@/utilities/polymorphic' | ||
import type { HeadingProps } from 'react-aria-components' | ||
|
||
import React from 'react' | ||
import { Heading } from 'react-aria-components' | ||
|
||
import { useAlertContext } from '@/components/alert/use-alert.hook' | ||
|
||
export type AlertHeadingProps = Component<'h3'> | ||
const __ELEMENT_TYPE__ = 'h1' | ||
|
||
const AlertHeading = React.forwardRef<HTMLHeadingElement, AlertHeadingProps>((props, ref) => { | ||
const { as, children, className, ...rest } = props | ||
const { slots } = useAlertContext() | ||
type ComponentOwnProps = HeadingProps | ||
|
||
const Component = as || 'h3' | ||
type ComponentProps<T extends React.ElementType> = Polymophic.ComponentPropsWithRef<T, ComponentOwnProps> | ||
|
||
const getProps = React.useCallback( | ||
() => ({ | ||
ref, | ||
className: slots.heading({ | ||
class: className, | ||
}), | ||
...rest, | ||
}), | ||
[ref, slots, className, rest] | ||
) | ||
type ComponentType = <T extends React.ElementType = typeof __ELEMENT_TYPE__>( | ||
props: ComponentProps<T> | ||
) => React.ReactNode | ||
|
||
const Component: ComponentType = React.forwardRef( | ||
<T extends React.ElementType = typeof __ELEMENT_TYPE__>(props: ComponentProps<T>, ref: Polymophic.Ref<T>) => { | ||
const { as, children, className, level = 3, ...rest } = props | ||
|
||
return <Component {...getProps()}>{children}</Component> | ||
}) | ||
const Element = as ?? Heading | ||
|
||
AlertHeading.displayName = 'Alert.Heading' | ||
const { slots } = useAlertContext() | ||
|
||
export default AlertHeading | ||
const component = React.useMemo<React.ComponentPropsWithoutRef<typeof __ELEMENT_TYPE__>>( | ||
() => ({ | ||
level, | ||
className: slots.heading({ className }), | ||
...rest, | ||
}), | ||
[className, level, rest, slots] | ||
) | ||
|
||
return ( | ||
<Element {...component} ref={ref}> | ||
{children} | ||
</Element> | ||
) | ||
} | ||
) | ||
|
||
export type { ComponentOwnProps as AlertHeadingProps } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,42 @@ | ||
import type { Component } from '@/utilities/types' | ||
import type * as Polymophic from '@/utilities/polymorphic' | ||
|
||
import React from 'react' | ||
|
||
import { useAlertContext } from '@/components/alert/use-alert.hook' | ||
|
||
export type AlertItemProps = Component<'li'> | ||
const __ELEMENT_TYPE__ = 'li' | ||
|
||
const AlertItem = React.forwardRef<HTMLLIElement, AlertItemProps>((props, ref) => { | ||
const { as, children, className, ...rest } = props | ||
const { slots } = useAlertContext() | ||
type ComponentOwnProps = {} | ||
|
||
const Component = as || 'li' | ||
type ComponentProps<T extends React.ElementType> = Polymophic.ComponentPropsWithRef<T, ComponentOwnProps> | ||
|
||
const getProps = React.useCallback( | ||
() => ({ | ||
ref, | ||
className: slots.item({ | ||
class: className, | ||
}), | ||
...rest, | ||
}), | ||
[ref, slots, className, rest] | ||
) | ||
type ComponentType = <T extends React.ElementType = typeof __ELEMENT_TYPE__>( | ||
props: ComponentProps<T> | ||
) => React.ReactNode | ||
|
||
const Component: ComponentType = React.forwardRef( | ||
<T extends React.ElementType = typeof __ELEMENT_TYPE__>(props: ComponentProps<T>, ref: Polymophic.Ref<T>) => { | ||
const { as, children, className, ...rest } = props | ||
|
||
return ( | ||
<Component role="listitem" {...getProps()}> | ||
{children} | ||
</Component> | ||
) | ||
}) | ||
const Element = as ?? __ELEMENT_TYPE__ | ||
|
||
AlertItem.displayName = 'Alert.Item' | ||
const { slots } = useAlertContext() | ||
|
||
export default AlertItem | ||
const component = React.useMemo<React.ComponentPropsWithoutRef<typeof __ELEMENT_TYPE__>>( | ||
() => ({ | ||
className: slots.item({ className }), | ||
...rest, | ||
}), | ||
[className, rest, slots] | ||
) | ||
|
||
return ( | ||
<Element {...component} ref={ref}> | ||
{children} | ||
</Element> | ||
) | ||
} | ||
) | ||
|
||
export type { ComponentOwnProps as AlertItemProps } | ||
export default Component |
Oops, something went wrong.