Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d95b2f9778eca1dac0887ea209223645c1958b207b73803e7a0bbeb1487ae53c142b2ec8cf28b23b3fb1f742094e6c77b65683bc075b602a7c427b9f2efa0d66d0ecb1801545cc36c5decb1484d7e66df634a1036f04f57a664932f6bcc88dd28ce582ab9a4b8ffdbd41477d006ae49546952dd90776ed24633204c3f7e0b3cbc20e8e9312b2913cb268ca7b267943e2b47b69207f275557f2e658fc1452e6cbbf9d7c05d5d688d255404175bcebe07b1ef28c42c45b6ae78cc2b24955178e221bb77600File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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} | ||
ContributorAuthor 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 Contributor 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 | ||
Member 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? ContributorAuthor 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. ContributorAuthor 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.
Uh oh!
There was an error while loading. Please reload this page.
| 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} |
Uh oh!
There was an error while loading. Please reload this page.
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
asprop from these types since I added it toPolymorphicProps.