-
-
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.
System: Initial work on \
forwardRef\
system.
This is heavily based on NextUI, kudos to them!
- Loading branch information
Showing
6 changed files
with
86 additions
and
25 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
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,24 +1,22 @@ | ||
import type { ElementType, HTMLProps } from 'react'; | ||
import { forwardRef } from '@nordcom/nordstar-system'; | ||
import styles from './heading.module.scss'; | ||
|
||
export type HeadingProps = { | ||
subheading?: boolean; | ||
level?: 'h1' | 'h2' | 'h3' | 'h4'; | ||
as?: ElementType; | ||
} & HTMLProps<HTMLHeadingElement>; | ||
|
||
const Heading = (props: HeadingProps) => { | ||
const { as, subheading, children, className } = props; | ||
const Heading = forwardRef<'h1', HeadingProps>((props, ref) => { | ||
const { as, level = 'h1', className } = props; | ||
|
||
const Tag = as || subheading ? 'h2' : 'h1'; | ||
const classes = `${styles.container} ${subheading ? styles.subheading : styles.heading}${ | ||
className ? ` ${className}` : '' | ||
}`; | ||
const Tag = as || level; | ||
const classes = `${styles.container} ${styles[level]}${className ? ` ${className}` : ''}`; | ||
|
||
return ( | ||
<Tag {...{ ...props, subheading: undefined }} className={classes}> | ||
{children} | ||
</Tag> | ||
); | ||
}; | ||
// TODO: Figure out a better way to exclude props from being passed to the DOM element. | ||
return <Tag {...{ ...props, level: undefined, as: undefined, className: undefined }} className={classes} />; | ||
}); | ||
|
||
Heading.displayName = 'Nordstar.Heading'; | ||
|
||
export default Heading; |
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 +1,5 @@ | ||
// Utilities | ||
export * from './utils'; | ||
|
||
// Components | ||
export * from './nordstar-provider'; |
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,56 @@ | ||
import { forwardRef as baseForwardRef } from 'react'; | ||
|
||
export type PropsOf<T extends As> = React.ComponentPropsWithoutRef<T> & { | ||
as?: As; | ||
}; | ||
|
||
export type OmitCommonProps<Target, OmitAdditionalProps extends keyof any = never> = Omit< | ||
Target, | ||
'as' | OmitAdditionalProps | ||
>; | ||
|
||
export type RightJoinProps<SourceProps extends object = {}, OverrideProps extends object = {}> = OmitCommonProps< | ||
SourceProps, | ||
keyof OverrideProps | ||
> & | ||
OverrideProps; | ||
|
||
export type As<Props = any> = React.ElementType<Props>; | ||
export type MergeWithAs< | ||
ComponentProps extends object, | ||
AsProps extends object, | ||
AdditionalProps extends object = {}, | ||
AsComponent extends As = As | ||
> = (RightJoinProps<ComponentProps, AdditionalProps> | RightJoinProps<AsProps, AdditionalProps>) & { | ||
as?: AsComponent; | ||
}; | ||
|
||
export type InternalForwardRefRenderFunction< | ||
Component extends As, | ||
Props extends object = {}, | ||
OmitKeys extends keyof any = never | ||
> = { | ||
<AsComponent extends As = Component>( | ||
props: MergeWithAs< | ||
React.ComponentPropsWithoutRef<Component>, | ||
Omit<React.ComponentPropsWithoutRef<AsComponent>, OmitKeys>, | ||
Props, | ||
AsComponent | ||
> | ||
): React.ReactElement | null; | ||
readonly $$typeof: symbol; | ||
defaultProps?: Partial<Props> | undefined; | ||
propTypes?: React.WeakValidationMap<Props> | undefined; | ||
displayName?: string | undefined; | ||
}; | ||
|
||
export function forwardRef<Component extends As, Props extends object, OmitKeys extends keyof any = never>( | ||
component: React.ForwardRefRenderFunction< | ||
any, | ||
RightJoinProps<PropsOf<Component>, Props> & { | ||
as?: As; | ||
} | ||
> | ||
) { | ||
return baseForwardRef(component) as InternalForwardRefRenderFunction<Component, Props, OmitKeys>; | ||
} |