Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,32 @@ describe('Radio Accessibility', () => {
expect(radio).toHaveProperty('checked', false)
})

it('does not pass both checked and defaultChecked to the input', () => {
const consoleError = vi
.spyOn(console, 'error')
.mockImplementation(() => {})

render(
<Radio
value="controlled"
checked={false}
defaultChecked
onChange={() => {}}
>
Controlled radio
</Radio>
)

const radio = screen.getByRole('radio', {
name: 'Controlled radio',
})

expect(radio).not.toBeChecked()
expect(consoleError).not.toHaveBeenCalled()

consoleError.mockRestore()
Comment on lines +273 to +291
})

it('announces disabled state - state programmatically determinable (WCAG 4.1.2)', () => {
const props = RadioTestFactory.disabled()
render(<Radio {...props} />)
Expand Down
17 changes: 12 additions & 5 deletions packages/blend/lib/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getTruncatedText } from '../../global-utils/GlobalUtils'
export const Radio = ({
id,
checked,
defaultChecked = false,
defaultChecked,
onChange,
disabled = false,
required = false,
Expand Down Expand Up @@ -45,12 +45,20 @@ export const Radio = ({
subtextId && customAriaDescribedBy
? `${customAriaDescribedBy} ${subtextId}`
: subtextId || customAriaDescribedBy
const isControlled = checked !== undefined
const checkedProps = isControlled
? { checked }
: { defaultChecked: defaultChecked ?? false }

return (
<Block
data-radio={children ?? 'radio'}
data-status={disabled ? 'disabled' : 'enabled'}
data-state={checked ? 'checked' : 'unchecked'}
data-state={
(isControlled ? checked : defaultChecked)
? 'checked'
: 'unchecked'
}
data-id={value ?? ''}
display="flex"
alignItems={subtext ? 'flex-start' : 'center'}
Expand All @@ -60,14 +68,13 @@ export const Radio = ({
type="radio"
id={uniqueId}
name={name}
checked={checked}
defaultChecked={defaultChecked}
{...checkedProps}
disabled={disabled}
required={required}
readOnly={isControlled && !onChange ? true : undefined}
onChange={onChange}
size={size}
$isDisabled={disabled}
$isChecked={checked || false}
$error={error}
$tokens={radioTokens}
style={getErrorShakeStyle(shouldShake)}
Expand Down
50 changes: 33 additions & 17 deletions packages/blend/lib/components/Radio/StyledRadio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { radioAnimations } from './radio.animations'
export const StyledRadioInput = styled.input<{
size: RadioSize
$isDisabled: boolean
$isChecked: boolean
$error?: boolean
$tokens: RadioTokensType
}>`
Comment on lines 7 to 12
Expand All @@ -22,17 +21,17 @@ export const StyledRadioInput = styled.input<{
padding: 0;
flex-shrink: 0;

${({ size, $isChecked, $isDisabled, $tokens }) => {
${({ size, $isDisabled, $tokens }) => {
const state = $isDisabled ? 'disabled' : 'default'
const indicatorState = $isChecked ? 'active' : 'inactive'
const inactiveIndicator = $tokens.indicator.inactive
const activeIndicator = $tokens.indicator.active

return css`
${radioAnimations}

background-color: ${$tokens.indicator[indicatorState]
.backgroundColor[state]};
border: ${$tokens.borderWidth[indicatorState][state]}px solid
${$tokens.indicator[indicatorState].borderColor[state]};
background-color: ${inactiveIndicator.backgroundColor[state]};
border: ${$tokens.borderWidth.inactive[state]}px solid
${inactiveIndicator.borderColor[state]};
width: ${$tokens.height[size]};
height: ${$tokens.height[size]};

Expand All @@ -41,28 +40,45 @@ export const StyledRadioInput = styled.input<{
width: 50%;
height: 50%;
border-radius: 50%;
background-color: ${$isChecked
? $tokens.activeIndicator.active.backgroundColor[state]
: 'transparent'};
transform: ${$isChecked ? 'scale(1)' : 'scale(0)'};
background-color: transparent;
transform: scale(0);
transition:
transform 250ms cubic-bezier(0.4, 0, 0.2, 1),
background-color 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

&:checked {
background-color: ${activeIndicator.backgroundColor[state]};
border: ${$tokens.borderWidth.active[state]}px solid
${activeIndicator.borderColor[state]};

&::after {
background-color: ${$tokens.activeIndicator.active
.backgroundColor[state]};
transform: scale(1);
}
}

&:focus-visible {
outline: 2px solid
${$tokens.indicator[indicatorState].borderColor[state]};
outline: 2px solid ${inactiveIndicator.borderColor[state]};
outline-offset: 2px;
/* WCAG 2.4.7 Focus Visible (AA): Focus indicator must be visible
* WCAG 1.4.11 Non-text Contrast (AA): Focus outline must have contrast ratio ≥3:1 against adjacent colors
* Manual verification recommended for all states */
}

&:checked:focus-visible {
outline-color: ${activeIndicator.borderColor[state]};
}

&:not(:disabled):hover {
background-color: ${$tokens.indicator[indicatorState]
.backgroundColor.hover};
border-color: ${$tokens.indicator[indicatorState].borderColor
.hover};
background-color: ${inactiveIndicator.backgroundColor.hover};
border-color: ${inactiveIndicator.borderColor.hover};
}

&:checked:not(:disabled):hover {
background-color: ${activeIndicator.backgroundColor.hover};
border-color: ${activeIndicator.borderColor.hover};
}

cursor: ${$isDisabled ? 'not-allowed' : 'pointer'};
Expand Down
Loading