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 FormControl to use CSS Modules behind flag #5438

Merged
merged 17 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
967127d
refactor: move form control test files into folder
joshblack Dec 5, 2024
38adc0c
refactor(FormControl): update FormControl to use CSS Modules behind flag
joshblack Dec 5, 2024
219f378
refactor(InputLabel): update InputLabel to use CSS Modules behind fea…
joshblack Dec 5, 2024
feb488f
refactor(FormControl): update Caption to use CSS Modules behind featu…
joshblack Dec 5, 2024
496fdd4
refactor(InputValidation): refactor InputValidation to CSS Modules be…
joshblack Dec 5, 2024
caf815b
chore: add changeset
joshblack Dec 5, 2024
a944165
Apply suggestions from code review
joshblack Dec 6, 2024
d42b0d1
chore: update stylelint violations
joshblack Dec 6, 2024
775aa56
fix: update selector for leading visual
joshblack Dec 6, 2024
5e1e74c
Merge branch 'main' of github.com:primer/react into refactor/update-f…
joshblack Dec 9, 2024
6491edb
Merge branch 'main' into refactor/update-form-control-to-css-modules-3
joshblack Dec 11, 2024
a6613aa
Merge branch 'main' into refactor/update-form-control-to-css-modules-3
joshblack Dec 13, 2024
08af355
Merge branch 'main' of https://github.com/primer/react into refactor/…
hussam-i-am Dec 18, 2024
8ee65ff
Merge branch 'main' into refactor/update-form-control-to-css-modules-3
hussam-i-am Dec 18, 2024
ab12bde
add missing sx prop
hussam-i-am Dec 18, 2024
137f19b
Merge https://github.com/primer/react into refactor/update-form-contr…
hussam-i-am Dec 18, 2024
bb0a9da
Merge branch 'refactor/update-form-control-to-css-modules-3' of https…
hussam-i-am Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-stingrays-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Update FormControl to use CSS Modules behind feature flag
57 changes: 57 additions & 0 deletions packages/react/src/FormControl/FormControl.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.ControlHorizontalLayout {
display: flex;

&:where([data-has-leading-visual]) {
align-items: center;
}
}

.ControlVerticalLayout {
display: flex;
flex-direction: column;
align-items: flex-start;

& > *:not(label) + * {
margin-top: var(--base-size-4);
}

&[data-has-label] > * + * {
margin-top: var(--base-size-4);
}
}

.ControlChoiceInputs > input {
margin-right: 0;
margin-left: 0;
}

.LabelContainer {
> * {
/* stylelint-disable-next-line primer/spacing */
padding-left: var(--stack-gap-condensed);
}

> label {
font-weight: var(--base-text-weight-normal);
}
}

.LeadingVisual {
margin-left: var(--base-size-8);
color: var(--fgColor-muted);

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

> * {
min-width: var(--text-body-size-large);
min-height: var(--text-body-size-large);
fill: currentColor;
}

> *:where([data-has-caption]) {
min-width: var(--base-size-24);
min-height: var(--base-size-24);
}
}
186 changes: 134 additions & 52 deletions packages/react/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {clsx} from 'clsx'
import React, {useContext} from 'react'
import Autocomplete from '../Autocomplete'
import Box from '../Box'
import Checkbox from '../Checkbox'
import Radio from '../Radio'
import Select from '../Select/Select'
Expand All @@ -10,7 +10,6 @@ import TextInputWithTokens from '../TextInputWithTokens'
import Textarea from '../Textarea'
import {CheckboxOrRadioGroupContext} from '../internal/components/CheckboxOrRadioGroup'
import ValidationAnimationContainer from '../internal/components/ValidationAnimationContainer'
import {get} from '../constants'
import {useSlots} from '../hooks/useSlots'
import type {SxProp} from '../sx'
import {useId} from '../hooks/useId'
Expand All @@ -20,6 +19,12 @@ import FormControlLeadingVisual from './FormControlLeadingVisual'
import FormControlValidation from './_FormControlValidation'
import {FormControlContextProvider} from './_FormControlContext'
import {warning} from '../utils/warning'
import styled from 'styled-components'
import sx from '../sx'
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent'
import {cssModulesFlag} from './feature-flags'
import {useFeatureFlag} from '../FeatureFlags'
import classes from './FormControl.module.css'

export type FormControlProps = {
children?: React.ReactNode
Expand All @@ -45,6 +50,7 @@ export type FormControlProps = {

const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
({children, disabled: disabledProp, layout = 'vertical', id: idProp, required, sx, className}, ref) => {
const enabled = useFeatureFlag(cssModulesFlag)
const [slots, childrenWithoutSlots] = useSlots(children, {
caption: FormControlCaption,
label: FormControlLabel,
Expand Down Expand Up @@ -127,69 +133,62 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
}}
>
{isChoiceInput || layout === 'horizontal' ? (
<Box
<StyledHorizontalLayout
ref={ref}
display="flex"
alignItems={slots.leadingVisual ? 'center' : undefined}
data-has-leading-visual={slots.leadingVisual ? '' : undefined}
sx={sx}
className={className}
className={clsx(className, {
[classes.ControlHorizontalLayout]: enabled,
})}
>
<Box sx={{'> input': {marginLeft: 0, marginRight: 0}}}>
{React.isValidElement(InputComponent) &&
React.cloneElement(
InputComponent as React.ReactElement<{
id: string
disabled: boolean
required: boolean
['aria-describedby']: string
}>,
{
id,
disabled,
// allow checkboxes to be required
required: required && !isRadioInput,
['aria-describedby']: captionId as string,
},
)}
<StyledChoiceInputs className={classes.ControlChoiceInputs}>
{React.isValidElement(InputComponent)
? React.cloneElement(
InputComponent as React.ReactElement<{
id: string
disabled: boolean
required: boolean
['aria-describedby']: string
}>,
{
id,
disabled,
// allow checkboxes to be required
required: required && !isRadioInput,
['aria-describedby']: captionId as string,
},
)
: null}
{childrenWithoutSlots.filter(
child =>
React.isValidElement(child) &&
![Checkbox, Radio].some(inputComponent => child.type === inputComponent),
)}
</Box>
{slots.leadingVisual && (
<Box
color={disabled ? 'fg.muted' : 'fg.default'}
sx={{
'> *': {
minWidth: slots.caption ? get('fontSizes.4') : get('fontSizes.2'),
minHeight: slots.caption ? get('fontSizes.4') : get('fontSizes.2'),
fill: 'currentColor',
},
}}
ml={2}
</StyledChoiceInputs>
{slots.leadingVisual ? (
<StyledLeadingVisual
className={clsx({
[classes.LeadingVisual]: enabled,
})}
data-disabled={disabled ? '' : undefined}
data-has-caption={slots.caption ? '' : undefined}
>
{slots.leadingVisual}
</Box>
)}
<Box
sx={{
'> *': {paddingLeft: 'var(--stack-gap-condensed)'},
'> label': {fontWeight: 'var(--base-text-weight-normal)'},
}}
>
</StyledLeadingVisual>
) : null}
<StyledLabelContainer className={classes.LabelContainer}>
{slots.label}
{slots.caption}
</Box>
</Box>
</StyledLabelContainer>
</StyledHorizontalLayout>
) : (
<Box
<StyledVerticalLayout
ref={ref}
display="flex"
flexDirection="column"
alignItems="flex-start"
sx={{...(isLabelHidden ? {'> *:not(label) + *': {marginTop: 1}} : {'> * + *': {marginTop: 1}}), ...sx}}
className={className}
data-has-label={!isLabelHidden ? '' : undefined}
sx={sx}
className={clsx(className, {
[classes.ControlVerticalLayout]: enabled,
})}
>
{slots.label}
{React.isValidElement(InputComponent) &&
Expand All @@ -215,13 +214,96 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>(
<ValidationAnimationContainer show>{slots.validation}</ValidationAnimationContainer>
) : null}
{slots.caption}
</Box>
</StyledVerticalLayout>
)}
</FormControlContextProvider>
)
},
)

const StyledHorizontalLayout = toggleStyledComponent(
cssModulesFlag,
'div',
styled.div`
display: flex;

&:where([data-has-leading-visual]) {
align-items: center;
}

${sx}
`,
)

const StyledChoiceInputs = toggleStyledComponent(
cssModulesFlag,
'div',
styled.div`
> input {
margin-left: 0;
margin-right: 0;
}
`,
)

const StyledLabelContainer = toggleStyledComponent(
cssModulesFlag,
'div',
styled.div`
> * {
padding-left: var(--stack-gap-condensed);
}

> label {
font-weight: var(--base-text-weight-normal);
}
`,
)

const StyledVerticalLayout = toggleStyledComponent(
cssModulesFlag,
'div',
styled.div`
display: flex;
flex-direction: column;
align-items: flex-start;

& > *:not(label) + * {
margin-top: var(--base-size-4);
}

&:where([data-has-label]) > * + * {
margin-top: var(--base-size-4);
}

${sx}
`,
)

const StyledLeadingVisual = toggleStyledComponent(
cssModulesFlag,
'div',
styled.div`
color: var(--fgColor-default);
margin-left: var(--base-size-8);

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

> * {
fill: currentColor;
min-width: var(--text-body-size-large);
min-height: var(--text-body-size-large);
}

> *:where([data-has-caption]) {
min-width: var(--base-size-24);
min-height: var(--base-size-24);
}
`,
)

export default Object.assign(FormControl, {
Caption: FormControlCaption,
Label: FormControlLabel,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.Caption {
display: block;
font-size: var(--text-body-size-small);
color: var(--fgColor-muted);

&:where([data-control-disabled]) {
color: var(--control-fgColor-disabled);
}
}
50 changes: 33 additions & 17 deletions packages/react/src/FormControl/FormControlCaption.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import {clsx} from 'clsx'
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 {cssModulesFlag} from './feature-flags'
import {useFeatureFlag} from '../FeatureFlags'
import Text from '../Text'
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}
`
import type {SxProp} from '../sx'
import classes from './FormControlCaption.module.css'
import {useFormControlContext} from './_FormControlContext'
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent'

type FormControlCaptionProps = React.PropsWithChildren<
{
Expand All @@ -25,12 +17,36 @@ type FormControlCaptionProps = React.PropsWithChildren<
>

function FormControlCaption({id, children, sx}: FormControlCaptionProps) {
const enabled = useFeatureFlag(cssModulesFlag)
const {captionId, disabled} = useFormControlContext()
return (
<StyledCaption id={id ?? captionId} data-control-disabled={disabled ? '' : undefined} sx={sx}>
<StyledCaption
id={id ?? captionId}
className={clsx({
[classes.Caption]: enabled,
})}
data-control-disabled={disabled ? '' : undefined}
sx={sx}
>
{children}
</StyledCaption>
)
}

const StyledCaption = toggleStyledComponent(
cssModulesFlag,
Text,
styled(Text)`
color: var(--fgColor-muted);
display: block;
font-size: var(--text-body-size-small);

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

${sx}
`,
)

export {FormControlCaption}
Loading
Loading