Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `Popover`: Default the popup's portal container to the `@wordpress/ui` compat overlay slot when present, so popovers stack reliably above other overlays in mixed-library compositions. A caller-supplied `Popover.Portal` `container` prop continues to take precedence ([#80278](https://github.com/WordPress/gutenberg/pull/80278)).
- `Button`: Add Storybook example demonstrating manual keyboard shortcut composition with `Tooltip`, `aria-keyshortcuts`, and an accessible description ([#80353](https://github.com/WordPress/gutenberg/pull/80353)).
- `Dialog`: Add Storybook usage guidelines for choosing between `Dialog` and `Drawer` ([#80783](https://github.com/WordPress/gutenberg/pull/80783)).
- Add `Radio` form primitive. ([#80799](https://github.com/WordPress/gutenberg/pull/80799)).

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/form/primitives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { InputLayout } from './input-layout';
export * as Select from './select';
export { SearchableChipSelect } from './searchable-chip-select';
export { Textarea } from './textarea';
export { Radio } from './radio';
1 change: 1 addition & 0 deletions packages/ui/src/form/primitives/radio/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Radio } from './radio';
27 changes: 27 additions & 0 deletions packages/ui/src/form/primitives/radio/radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Radio as _Radio } from '@base-ui/react/radio';
import clsx from 'clsx';
import { forwardRef } from '@wordpress/element';
import resetStyles from '../../../utils/css/resets.module.css';
import focusStyles from '../../../utils/css/focus.module.css';
import styles from './style.module.css';
import type { RadioProps } from './types';

export const Radio = forwardRef< HTMLSpanElement, RadioProps >( function Radio(
{ className, ...restProps },
ref
) {
return (
<_Radio.Root
ref={ ref }
className={ clsx(
resetStyles[ 'box-sizing' ],
focusStyles[ 'outset-ring--focus' ],
styles.root,
className
) }
{ ...restProps }
>
<_Radio.Indicator className={ styles.indicator } />
</_Radio.Root>
);
} );
65 changes: 65 additions & 0 deletions packages/ui/src/form/primitives/radio/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { RadioGroup } from '@base-ui/react/radio-group';
import { Radio } from '../';

const meta: Meta< typeof Radio > = {
title: 'Design System/Components/Form/Primitives/Radio',
component: Radio,
parameters: {
componentStatus: {
status: 'use-with-caution',
whereUsed: 'global',
notes: 'Not yet recommended for use alongside components from `@wordpress/components`, pending review of style consistency with `@wordpress/components` and component set completeness. See [WordPress/gutenberg#76135](https://github.com/WordPress/gutenberg/issues/76135). Must be rendered inside a `RadioGroup` (coming to the package later).',
},
},
};

export default meta;

type Story = StoryObj< typeof Radio >;

export const Default: Story = {
render: ( args ) => (
<RadioGroup>
<Radio { ...args } />
</RadioGroup>
),
args: {
value: 'option',
},
};

export const Checked: Story = {
render: ( args ) => (
<RadioGroup defaultValue="option">
<Radio { ...args } />
</RadioGroup>
),
args: {
value: 'option',
},
};

export const Disabled: Story = {
render: ( args ) => (
<RadioGroup>
<Radio { ...args } />
</RadioGroup>
),
args: {
value: 'option',
disabled: true,
},
};

export const DisabledChecked: Story = {
render: ( args ) => (
<RadioGroup defaultValue="option">
<Radio { ...args } />
</RadioGroup>
),
args: {
value: 'option',
disabled: true,
},
};
73 changes: 73 additions & 0 deletions packages/ui/src/form/primitives/radio/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
@layer wp-ui {
@layer utilities, components, compositions, overrides;

@layer components {
.root {
--wp-ui-radio-input-size: 16px;
--wp-ui-radio-indicator-size: 8px;

position: relative;
display: inline-flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: var(--wp-ui-radio-input-size);
height: var(--wp-ui-radio-input-size);
border-radius: 50%;
border:
var(--wpds-border-width-xs) solid
var(--wpds-color-stroke-interactive-neutral);
padding: 0;
background-color: var(--wpds-color-background-interactive-neutral-weak);

&:not([data-disabled]) {
cursor: var(--wpds-cursor-control);
}

[data-highlighted] &,
&:hover {
border-color: var(--wpds-color-stroke-interactive-neutral-active);
}

&[data-checked] {
border-color: transparent;
background-color: var(--wpds-color-background-interactive-brand-strong);
color: var(--wpds-color-foreground-interactive-brand-strong);
}

&[data-disabled] {
background-color: var(--wpds-color-background-interactive-neutral-weak-disabled);
border-color: var(--wpds-color-stroke-interactive-neutral-disabled);
color: var(--wpds-color-foreground-interactive-neutral-disabled);

@media (forced-colors: active) {
color: GrayText;
border-color: GrayText;
}
}
}

.indicator {
display: flex;
align-items: center;
justify-content: center;
/* Ignore the border width and take up the full 16px */
position: absolute;
inset: calc(0px - var(--wpds-border-width-xs));

&::before {
content: "";
width: var(--wp-ui-radio-indicator-size);
height: var(--wp-ui-radio-indicator-size);
border-radius: 50%;
background-color: currentColor;

/* Serves as a background color in Windows High Contrast mode. */
@media (forced-colors: active) {
border: 4px solid currentColor;
background-color: transparent;
}
}
}
}
}
38 changes: 38 additions & 0 deletions packages/ui/src/form/primitives/radio/test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render, screen } from '@testing-library/react';
import { RadioGroup } from '@base-ui/react/radio-group';
import { createRef } from '@wordpress/element';
import { Radio } from '../index';

describe( 'Radio', () => {
it( 'forwards ref', () => {
const ref = createRef< HTMLSpanElement >();

render(
<RadioGroup>
<Radio ref={ ref } value="a" />
</RadioGroup>
);

expect( ref.current ).toBeInstanceOf( HTMLSpanElement );
} );

it( 'renders a radio', () => {
render(
<RadioGroup>
<Radio value="a" />
</RadioGroup>
);

expect( screen.getByRole( 'radio' ) ).toBeInTheDocument();
} );

it( 'renders checked when selected by the radio group', () => {
render(
<RadioGroup defaultValue="a">
<Radio value="a" />
</RadioGroup>
);

expect( screen.getByRole( 'radio' ) ).toBeChecked();
} );
} );
4 changes: 4 additions & 0 deletions packages/ui/src/form/primitives/radio/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Radio as _Radio } from '@base-ui/react/radio';
import type { ComponentProps } from '../../../utils/types';

export type RadioProps = ComponentProps< typeof _Radio.Root >;
Loading