diff --git a/src/lib/chip/chip.tsx b/src/lib/chip/chip.tsx index f24c9d6f..66248267 100644 --- a/src/lib/chip/chip.tsx +++ b/src/lib/chip/chip.tsx @@ -23,7 +23,7 @@ export const Chip = forwardRef(function Chip( const selectable = typeof props['aria-selected'] !== 'undefined'; return ( = PolyProps; -type HeadingComponent = (props: HeadingProps) => React.ReactElement | null; +interface HeadingProps extends HeadingVariants { + disabled?: boolean; +} -export const Heading: HeadingComponent = forwardRef( - ( - { as, variant = 'default', level = '1', dense = true, className, ...props }: HeadingProps, - ref: PolyRef - ) => { - const Component = as || 'h1'; +export const Heading = forwardRef( + ({ as: Component = 'h1', variant = 'default', level = '1', dense = true, className, ...props }, ref) => { return ; } -); +) as ForwardRefComponent<'h1', HeadingProps>; type HeadingVariants = VariantProps; const headingCss = css(styles.heading, { diff --git a/src/lib/menu-list/menu-list.tsx b/src/lib/menu-list/menu-list.tsx index f547822e..6db87295 100644 --- a/src/lib/menu-list/menu-list.tsx +++ b/src/lib/menu-list/menu-list.tsx @@ -1,13 +1,13 @@ import * as RovingFocusGroup from '@radix-ui/react-roving-focus'; import { clsx } from 'clsx'; -import { ComponentProps, ElementType, forwardRef, ReactElement } from 'react'; +import { ComponentProps, forwardRef } from 'react'; import { Flex } from '../flex'; import { useAllHandlers, useKeyboardHandles } from '../utils/hooks'; import { listItemCss, ListItemVariants } from '../shared/list-item'; -import type { PolyProps, PolyRef } from '../utils/polymorphic'; +import type { ForwardRefComponent } from '../utils/polymorphic'; import styles from './menu-list.module.css'; interface MenuListProps extends ComponentProps {} @@ -20,38 +20,31 @@ export const MenuList = ({ flow = 'column', className, ...props }: MenuListProps ); }; -type MenuListItemProps = PolyProps< - C, - ListItemVariants & { disabled?: boolean; active?: boolean } ->; -type MenuListItemComponent = (props: MenuListItemProps) => ReactElement | null; - -export const Item: MenuListItemComponent = forwardRef( - ( - { as, className, disabled, onKeyDown, active, ...props }: MenuListItemProps, - ref: PolyRef - ) => { - const Component = as || 'a'; - const onKeyDownHandler = useKeyboardHandles({ - 'Enter': (e) => e.currentTarget.click?.(), - ' ': (e) => e.currentTarget.click?.(), - }); - const handleKeyDown = useAllHandlers(onKeyDown, onKeyDownHandler); - - return ( - - - - ); - } -); +interface MenuListItemProps extends ListItemVariants { + disabled?: boolean; + active?: boolean; +} + +export const Item = forwardRef(({ as: Component = 'a', className, disabled, onKeyDown, active, ...props }, ref) => { + const onKeyDownHandler = useKeyboardHandles({ + 'Enter': (e) => e.currentTarget.click?.(), + ' ': (e) => e.currentTarget.click?.(), + }); + const handleKeyDown = useAllHandlers(onKeyDown, onKeyDownHandler); + + return ( + + + + ); +}) as ForwardRefComponent<'a', MenuListItemProps>; MenuList.Item = Item; diff --git a/src/lib/menu/menu.tsx b/src/lib/menu/menu.tsx index 80083822..9ceb9bd0 100644 --- a/src/lib/menu/menu.tsx +++ b/src/lib/menu/menu.tsx @@ -1,6 +1,5 @@ import { ComponentProps, - ElementType, forwardRef, ForwardRefExoticComponent, ReactElement, @@ -19,8 +18,6 @@ import { ListItem, ListItemVariants } from '../shared/list-item'; import { useIsomorphicLayoutEffect } from '../utils/hooks'; import { Presence } from '../shared/presence'; -import type { LabelComponent, LabelProps } from '../form-field/label'; -import type { PolyRef } from '../utils/polymorphic'; import styles from './menu.module.css'; interface MenuProps { @@ -112,15 +109,13 @@ export const Separator = (props: ComponentProps<'div'>) => { return
; }; -const MenuLabel: LabelComponent = forwardRef( - (props: LabelProps, ref: PolyRef) => { - return ( - - - ); - } -); +const MenuLabel = forwardRef((props, ref) => { + return ( + + + ); +}) as typeof Label; MenuRoot.List = List; MenuRoot.Item = MenuItem; diff --git a/src/lib/shared/list-item.tsx b/src/lib/shared/list-item.tsx index 517337df..362bfbd7 100644 --- a/src/lib/shared/list-item.tsx +++ b/src/lib/shared/list-item.tsx @@ -1,20 +1,16 @@ -import { ElementType, forwardRef, ReactElement } from 'react'; +import { forwardRef } from 'react'; import { classed as css, VariantProps } from '@tw-classed/core'; import { clsx } from 'clsx'; import { flexCss, FlexVariants } from '../flex/flex'; -import type { PolyProps, PolyRef } from '../utils/polymorphic'; +import type { ForwardRefComponent } from '../utils/polymorphic'; import styles from './list-item.module.css'; -type ListItemProps = PolyProps; -type ListItemComponent = (props: ListItemProps) => ReactElement | null; +interface ListItemProps extends ListItemVariants {} -export const ListItem: ListItemComponent = forwardRef( - ({ as, className, ...props }: ListItemProps, ref: PolyRef) => { - const Component = as || 'div'; - return ; - } -); +export const ListItem = forwardRef(({ as: Component = 'div', className, ...props }, ref) => { + return ; +}) as ForwardRefComponent<'div', ListItemProps>; const listItemCssInner = css(styles['list-item'], flexCss, { variants: { diff --git a/src/lib/text/text.tsx b/src/lib/text/text.tsx index 233bedc1..6d557b89 100644 --- a/src/lib/text/text.tsx +++ b/src/lib/text/text.tsx @@ -1,29 +1,26 @@ -import { ElementType, forwardRef, ReactElement } from 'react'; +import { CSSProperties, forwardRef } from 'react'; import { classed as css, VariantProps } from '@tw-classed/core'; import { clsx } from 'clsx'; -import type { PolyProps, PolyRef } from '../utils/polymorphic'; +import type { ForwardRefComponent } from '../utils/polymorphic'; import styles from './text.module.css'; -type TextProps = PolyProps & { lineClamp?: number }>; -type TextComponent = (props: TextProps) => ReactElement | null; +interface TextProps extends VariantProps { + lineClamp?: number; +} -export const Text: TextComponent = forwardRef( - ( - { as, textStyle, tone, align, lineClamp, className, style, ...props }: TextProps, - ref: PolyRef - ) => { - const Component = as || 'span'; +export const Text = forwardRef( + ({ as: Component = 'span', textStyle, tone, align, lineClamp, className, style, ...props }, ref) => { return ( ); } -); +) as ForwardRefComponent<'span', TextProps>; export const textCss = css(styles.text, { variants: { diff --git a/src/lib/utils/polymorphic.ts b/src/lib/utils/polymorphic.ts index 1f4c6aaa..ed4425de 100644 --- a/src/lib/utils/polymorphic.ts +++ b/src/lib/utils/polymorphic.ts @@ -7,21 +7,6 @@ import type * as React from 'react'; -type PolymorphicRef = React.ComponentPropsWithRef['ref']; - -type AsProp = { - as?: C; -}; - -type PropsToOmit = keyof (AsProp & P); - -type PolymorphicComponentProp = React.PropsWithChildren> & - Omit, PropsToOmit>; - -type PolymorphicComponentPropWithRef = PolymorphicComponentProp & { - ref?: PolymorphicRef; -}; - type Merge = Omit & P2; type ForwardRefExoticComponent = React.ForwardRefExoticComponent< @@ -71,4 +56,4 @@ interface ForwardRefComponent< ): React.ReactElement | null; } -export type { PolymorphicComponentPropWithRef as PolyProps, PolymorphicRef as PolyRef, ForwardRefComponent }; +export type { ForwardRefComponent }; diff --git a/src/pages/components/TextField.mdx b/src/pages/components/TextField.mdx index 77c7c43e..d8d53c93 100644 --- a/src/pages/components/TextField.mdx +++ b/src/pages/components/TextField.mdx @@ -75,17 +75,6 @@ title: TextField - - - - - ```