Skip to content
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

refactor(FormControl): update existing code for sub-components to prep for migration #5342

Merged
merged 10 commits into from
Dec 4, 2024
5 changes: 5 additions & 0 deletions .changeset/soft-bananas-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Update FormControl sub-components to use new styled components format for migration
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@
"webpack": false,
"running": false
}
]
],
"packageManager": "[email protected]+sha512.c89530d37c4baa38afd43e76a077a84b9aa63840b986426584fd5c5a54ab0a0b21bb1595c851042b733784b0b43706d36a494b4d8ae1a086a762cb8d3f95942a"
joshblack marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 3 additions & 3 deletions packages/react/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {get} from '../constants'
import {useSlots} from '../hooks/useSlots'
import type {SxProp} from '../sx'
import {useId} from '../hooks/useId'
import FormControlCaption from './_FormControlCaption'
import FormControlLabel from './_FormControlLabel'
import FormControlLeadingVisual from './_FormControlLeadingVisual'
import {FormControlCaption} from './FormControlCaption'
import FormControlLabel from './FormControlLabel'
import FormControlLeadingVisual from './FormControlLeadingVisual'
import FormControlValidation from './_FormControlValidation'
import {FormControlContextProvider} from './_FormControlContext'
import {warning} from '../utils/warning'
Expand Down
36 changes: 36 additions & 0 deletions packages/react/src/FormControl/FormControlCaption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'
import Text from '../Text'
import styled from 'styled-components'
import {get} from '../constants'
import sx from '../sx'

const StyledCaption = styled(Text)`
color: var(--fgColor-muted);
display: block;
font-size: ${get('fontSizes.0')};

&:where([data-control-disabled]) {
color: var(--control-fgColor-disabled);
}

${sx}
`

type FormControlCaptionProps = React.PropsWithChildren<
{
id?: string
} & SxProp
>

function FormControlCaption({id, children, sx}: FormControlCaptionProps) {
const {captionId, disabled} = useFormControlContext()
return (
<StyledCaption id={id ?? captionId} data-control-disabled={disabled ? '' : undefined} sx={sx}>
{children}
</StyledCaption>
)
}

export {FormControlCaption}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import InputLabel from '../internal/components/InputLabel'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'
import {InputLabel} from '../internal/components/InputLabel'

export type Props = {
/**
Expand Down Expand Up @@ -49,6 +49,7 @@ const FormControlLabel: React.FC<
sx,
...props,
}

return <InputLabel {...labelProps}>{children}</InputLabel>
}

Expand Down
44 changes: 44 additions & 0 deletions packages/react/src/FormControl/FormControlLeadingVisual.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react'
import {get} from '../constants'
import type {SxProp} from '../sx'
import {useFormControlContext} from './_FormControlContext'
import styled from 'styled-components'
import sx from '../sx'

const FormControlLeadingVisual: React.FC<React.PropsWithChildren<SxProp>> = ({children, sx}) => {
const {disabled, captionId} = useFormControlContext()
return (
<StyledLeadingVisual
data-control-disabled={disabled ? '' : undefined}
data-has-caption={captionId ? '' : undefined}
sx={sx}
>
{children}
</StyledLeadingVisual>
)
}

const StyledLeadingVisual = styled.div`
--leadingVisual-size: ${get('fontSizes.2')};

color: var(--fgColor-default);
margin-inline-start: ${get('space.2')};

&:where([data-control-disabled]) {
color: var(--fgColor-muted);
}

& > * {
min-width: var(--leadingVisual-size);
min-height: var(--leadingVisual-size);
fill: currentColor;
}

&:where([data-has-caption]) {
--leadingVisual-size: ${get('fontSizes.4')};
}

${sx}
`

export default FormControlLeadingVisual
15 changes: 0 additions & 15 deletions packages/react/src/FormControl/_FormControlCaption.tsx

This file was deleted.

27 changes: 0 additions & 27 deletions packages/react/src/FormControl/_FormControlLeadingVisual.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {useSlots} from '../../hooks/useSlots'
import {useProvidedRefOrCreate, useId, useAnchoredPosition} from '../../hooks'
import type {OverlayProps} from '../../Overlay/Overlay'
import {StyledOverlay, heightMap} from '../../Overlay/Overlay'
import InputLabel from '../../internal/components/InputLabel'
import {InputLabel} from '../../internal/components/InputLabel'
import {invariant} from '../../utils/invariant'
import {AriaStatus} from '../../live-region'
import {useResponsiveValue} from '../../hooks/useResponsiveValue'
Expand Down
30 changes: 0 additions & 30 deletions packages/react/src/internal/components/InputCaption.tsx

This file was deleted.

75 changes: 50 additions & 25 deletions packages/react/src/internal/components/InputLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import Box from '../../Box'
import type {SxProp} from '../../sx'
import VisuallyHidden from '../../_VisuallyHidden'
import styled from 'styled-components'
import {get} from '../../constants'
import sx, {type SxProp} from '../../sx'

type BaseProps = SxProp & {
disabled?: boolean
Expand All @@ -23,9 +23,9 @@ export type LegendOrSpanProps = BaseProps & {
htmlFor?: undefined
}

type Props = LabelProps | LegendOrSpanProps
type Props = React.PropsWithChildren<LabelProps | LegendOrSpanProps>

const InputLabel: React.FC<React.PropsWithChildren<Props>> = ({
function InputLabel({
children,
disabled,
htmlFor,
Expand All @@ -38,37 +38,62 @@ const InputLabel: React.FC<React.PropsWithChildren<Props>> = ({
as = 'label',
className,
...props
}) => {
}: Props) {
return (
<VisuallyHidden
isVisible={!visuallyHidden}
as={
as as 'label' /* This assertion is clearly wrong, but it's the only way TS will allow the htmlFor prop to be possibly defined */
}
<StyledLabel
as={as}
data-control-disabled={disabled ? '' : undefined}
data-visually-hidden={visuallyHidden ? '' : undefined}
htmlFor={htmlFor}
id={id}
className={className}
sx={{
fontWeight: 'bold',
fontSize: 1,
display: 'block',
color: disabled ? 'fg.muted' : 'fg.default',
cursor: disabled ? 'not-allowed' : 'pointer',
alignSelf: 'flex-start',
...sx,
}}
sx={sx}
{...props}
>
{required || requiredText ? (
<Box display="flex" as="span">
<Box mr={1}>{children}</Box>
<StyledRequiredText>
<span>{children}</span>
<span aria-hidden={requiredIndicator ? undefined : true}>{requiredText ?? '*'}</span>
</Box>
</StyledRequiredText>
) : (
children
)}
</VisuallyHidden>
</StyledLabel>
)
}

export default InputLabel
const StyledRequiredText = styled.span`
display: flex;
column-gap: ${get('space.1')};
`

const StyledLabel = styled.label`
align-self: flex-start;
display: block;
color: var(--fgColor-default);
cursor: pointer;
font-weight: 600;
font-size: ${get('fontSizes.1')};

&:where([data-control-disabled]) {
color: var(--fgColor-muted);
cursor: not-allowed;
}

&:where([data-visually-hidden]) {
border: 0;
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
white-space: nowrap;
width: 1px;
}

${sx}
`

export {InputLabel}
Loading
Loading