-
Notifications
You must be signed in to change notification settings - Fork 638
Remove Box usage and sx
prop from Link and misc other areas (stories, etc)
#6825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d95b2f9
778eca1
dac0887
ea20922
3645c19
58b207b
73803e7
a0bbeb1
487ae53
c142b2e
c8cf28b
23b3fb1
f742094
e6c77b6
5683bc0
75b602a
7c427b9
f2efa0d
66d0ecb
1801545
cc36c5d
ecb1484
d7e66df
634a103
6f04f57
a664932
f6bcc88
dd28ce5
82ab9a4
b8ffdbd
41477d0
06ae495
46952dd
90776ed
2463320
4c3f7e0
b3cbc20
e8e9312
b2913cb
268ca7b
267943e
2b47b69
207f275
557f2e6
58fc145
2e6cbbf
9d7c05d
5d688d2
5540417
5bcebe0
7b1ef28
c42c45b
6ae78cc
2b24955
178e221
bb77600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': major | ||
--- | ||
|
||
Removes `Box` component usage and `sx` prop from the `Link` component, Storybook stories, and a .figma.tsx file |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
import {clsx} from 'clsx' | ||
import React, {forwardRef, useEffect} from 'react' | ||
import React, {useEffect, type ForwardedRef, type ElementRef} from 'react' | ||
import {useRefObjectAsForwardedRef} from '../hooks' | ||
import type {SxProp} from '../sx' | ||
import classes from './Link.module.css' | ||
import Box from '../Box' | ||
import type {ComponentProps} from '../utils/types' | ||
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
import {type PolymorphicProps, fixedForwardRef} from '../utils/modern-polymorphic' | ||
|
||
type StyledLinkProps = { | ||
type StyledLinkProps<As extends React.ElementType = 'a'> = { | ||
as?: As | ||
/** @deprecated use CSS modules to style hover color */ | ||
hoverColor?: string | ||
muted?: boolean | ||
/** @deprecated use `inline` to specify the type of link instead */ | ||
underline?: boolean | ||
// Link inside a text block | ||
inline?: boolean | ||
} & SxProp | ||
} | ||
|
||
const Link = forwardRef(({as: Component = 'a', className, inline, underline, hoverColor, ...props}, forwardedRef) => { | ||
const innerRef = React.useRef<HTMLAnchorElement>(null) | ||
useRefObjectAsForwardedRef(forwardedRef, innerRef) | ||
export const UnwrappedLink = <As extends React.ElementType = 'a'>( | ||
props: PolymorphicProps<As, 'a', StyledLinkProps>, | ||
ref: ForwardedRef<unknown>, | ||
) => { | ||
const {as: Component = 'a', className, inline, underline, hoverColor, ...restProps} = props | ||
const innerRef = React.useRef<ElementRef<As>>(null) | ||
useRefObjectAsForwardedRef(ref, innerRef) | ||
|
||
if (__DEV__) { | ||
/** | ||
|
@@ -46,37 +49,23 @@ const Link = forwardRef(({as: Component = 'a', className, inline, underline, hov | |
}, [innerRef]) | ||
} | ||
|
||
if (props.sx) { | ||
return ( | ||
<Box | ||
as={Component} | ||
className={clsx(className, classes.Link)} | ||
data-muted={props.muted} | ||
data-inline={inline} | ||
data-underline={underline} | ||
data-hover-color={hoverColor} | ||
{...props} | ||
// @ts-ignore shh | ||
ref={innerRef} | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<Component | ||
className={clsx(className, classes.Link)} | ||
data-muted={props.muted} | ||
data-muted={restProps.muted} | ||
data-inline={inline} | ||
data-underline={underline} | ||
data-hover-color={hoverColor} | ||
{...props} | ||
// @ts-ignore shh | ||
ref={innerRef} | ||
{...restProps} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ref={innerRef as any} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I can get rid of this cast if I just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is old though. so i guess its an improvement to have it |
||
/> | ||
) | ||
}) as PolymorphicForwardRefComponent<'a', StyledLinkProps> | ||
} | ||
|
||
const LinkComponent = fixedForwardRef(UnwrappedLink) | ||
|
||
Link.displayName = 'Link' | ||
const Link = Object.assign(LinkComponent, {displayName: 'Link'}) | ||
|
||
export type LinkProps = ComponentProps<typeof Link> | ||
export default Link |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Mostly taken from https://github.com/total-typescript/react-typescript-tutorial/blob/main/src/08-advanced-patterns/72-as-prop-with-forward-ref.solution.tsx | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you elaborate on the need for this over the regular polymorphic we've been using? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our current approach causes the following problems:
@adierkens or somebody who is actually good at TypeScript can keep me correct on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All that said, I'm fine with going back to This just felt like a good opportunity to level-up our component types. |
||
|
||
import {forwardRef} from 'react' | ||
import type {ComponentPropsWithRef, ElementType} from 'react' | ||
|
||
/** | ||
* Distributive Omit utility type that works correctly with union types | ||
*/ | ||
type DistributiveOmit<T, TOmitted extends PropertyKey> = T extends unknown ? Omit<T, TOmitted> : never | ||
|
||
/** | ||
* Fixed version of forwardRef that provides better type inference for polymorphic components | ||
*/ | ||
// TODO: figure out how to change this type so we can set displayName | ||
// like this: `ComponentName.displayName = 'DisplayName' instead of using workarounds | ||
type FixedForwardRef = <T, P extends Record<string, unknown> = Record<string, unknown>>( | ||
render: (props: P, ref: React.Ref<T>) => React.ReactNode, | ||
) => (props: P & React.RefAttributes<T>) => React.ReactNode | ||
|
||
/** | ||
* Cast forwardRef to the fixed version with better type inference | ||
*/ | ||
export const fixedForwardRef = forwardRef as FixedForwardRef | ||
|
||
/** | ||
* Simplified polymorphic props type that handles the common pattern of | ||
* `DistributiveOmit<ComponentPropsWithRef<ElementType extends As ? DefaultElement : As>, 'as'>` | ||
*/ | ||
export type PolymorphicProps< | ||
TAs extends ElementType, | ||
TDefaultElement extends ElementType = 'div', | ||
Props extends Record<string, unknown> = Record<string, unknown>, | ||
> = DistributiveOmit<ComponentPropsWithRef<ElementType extends TAs ? TDefaultElement : TAs> & Props, 'as'> & { | ||
as?: TAs | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {Link as PrimerLink, type LinkProps as PrimerLinkProps} from '@primer/react' | ||
import styled from 'styled-components' | ||
import {sx, type SxProp} from '../sx' | ||
|
||
type LinkProps = PrimerLinkProps & SxProp | ||
|
||
const Link = styled(PrimerLink).withConfig<LinkProps>({ | ||
shouldForwardProp: prop => prop !== 'sx', | ||
})` | ||
${sx} | ||
` | ||
export {Link, type LinkProps} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I can remove the
as
prop from these types since I added it toPolymorphicProps
.