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

Font Library modal: Try to improve checkbox labelling #58339

Merged
merged 13 commits into from
Feb 2, 2024
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- `DropdownMenuV2`: Restore accent color themability ([#58130](https://github.com/WordPress/gutenberg/pull/58130)).
- Expand theming support in the `COLORS` variable object ([#58097](https://github.com/WordPress/gutenberg/pull/58097)).

### Enhancements

- `CheckboxControl`: Remove ability for label prop to be false ([#58339](https://github.com/WordPress/gutenberg/pull/58339)).

## 25.16.0 (2024-01-24)

### Enhancements
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/checkbox-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ const MyCheckboxControl = () => {
The set of props accepted by the component will be specified below.
Props not included in this set will be applied to the input element.

#### `label`: `string|false`
#### `label`: `string`

A label for the input field, that appears at the side of the checkbox.
The prop will be rendered as content a label element.
If no prop is passed an empty label is rendered.
If the prop is set to false no label is rendered.

- Required: No

Expand Down
14 changes: 6 additions & 8 deletions packages/components/src/checkbox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ export function CheckboxControl(
/>
) : null }
</span>
{ label && (
<label
className="components-checkbox-control__label"
htmlFor={ id }
>
{ label }
</label>
) }
<label
className="components-checkbox-control__label"
htmlFor={ id }
>
{ label }
</label>
</BaseControl>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Snapshot Diff:
- First value
+ Second value

@@ -8,13 +8,27 @@
@@ -8,17 +8,31 @@
<span
class="components-checkbox-control__input-container"
>
<input
class="components-checkbox-control__input"
- id="inspector-checkbox-control-6"
+ id="inspector-checkbox-control-7"
- id="inspector-checkbox-control-5"
+ id="inspector-checkbox-control-6"
type="checkbox"
value="1"
+ />
Expand All @@ -31,6 +31,11 @@ Snapshot Diff:
/>
+ </svg>
</span>
<label
class="components-checkbox-control__label"
- for="inspector-checkbox-control-5"
+ for="inspector-checkbox-control-6"
/>
</div>
</div>
</div>
Expand Down
7 changes: 0 additions & 7 deletions packages/components/src/checkbox-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ describe( 'CheckboxControl', () => {
expect( label ).toBeInTheDocument();
} );

it( 'should not render label element if label is set to false', () => {
render( <CheckboxControl label={ false } /> );

const label = screen.queryByRole( 'label' );
expect( label ).not.toBeInTheDocument();
} );

it( 'should render a checkbox in an indeterminate state', () => {
render( <CheckboxControl indeterminate /> );
expect( getInput() ).toHaveProperty( 'indeterminate', true );
Expand Down
5 changes: 2 additions & 3 deletions packages/components/src/checkbox-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ export type CheckboxControlProps = Pick<
/**
* A label for the input field, that appears at the side of the checkbox.
* The prop will be rendered as content a label element. If no prop is
* passed an empty label is rendered. If the prop is set to false no label
* is rendered.
* passed an empty label is rendered.
*/
label?: string | false;
label?: string;
/**
* If checked is true the checkbox will be checked. If checked is false the
* checkbox will be unchecked. If no value is passed the checkbox will be
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/**
* WordPress dependencies
*/
import {
CheckboxControl,
Flex,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { CheckboxControl, Flex } from '@wordpress/components';

/**
* Internal dependencies
*/
import { getFontFaceVariantName } from './utils';
import FontFaceDemo from './font-demo';
import { unlock } from '../../../lock-unlock';

function CollectionFontVariant( {
face,
Expand All @@ -29,27 +24,23 @@ function CollectionFontVariant( {
};

const displayName = font.name + ' ' + getFontFaceVariantName( face );
const { kebabCase } = unlock( componentsPrivateApis );
const checkboxId = kebabCase(
`${ font.slug }-${ getFontFaceVariantName( face ) }`
);

return (
<label
className="font-library-modal__library-font-variant"
htmlFor={ checkboxId }
>
<div className="font-library-modal__library-font-variant">
<Flex justify="flex-start" align="center" gap="1rem">
<CheckboxControl
checked={ selected }
onChange={ handleToggleActivation }
__nextHasNoMarginBottom={ true }
id={ checkboxId }
label={ false }
label={ displayName }
/>
<FontFaceDemo
fontFace={ face }
text={ displayName }
onClick={ handleToggleActivation }
/>
<FontFaceDemo fontFace={ face } text={ displayName } />
</Flex>
</label>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ function getPreviewUrl( fontFace ) {
}
}

function FontFaceDemo( { customPreviewUrl, fontFace, text, style = {} } ) {
function FontFaceDemo( {
customPreviewUrl,
fontFace,
text,
onClick,
style = {},
} ) {
const ref = useRef( null );
const [ isIntersecting, setIsIntersecting ] = useState( false );
const [ isAssetLoaded, setIsAssetLoaded ] = useState( false );
Expand All @@ -43,6 +49,9 @@ function FontFaceDemo( { customPreviewUrl, fontFace, text, style = {} } ) {
height: '23px',
width: 'auto',
};
const containerStyle = {
cursor: 'pointer',
};

useEffect( () => {
const observer = new window.IntersectionObserver( ( [ entry ] ) => {
Expand All @@ -65,7 +74,14 @@ function FontFaceDemo( { customPreviewUrl, fontFace, text, style = {} } ) {
}, [ fontFace, isIntersecting, loadFontFaceAsset, isPreviewImage ] );

return (
<div ref={ ref }>
<div
ref={ ref }
onClick={ onClick }
onKeyDown={ onClick }
tabIndex={ -1 }
role="button"
style={ containerStyle }
>
{ isPreviewImage ? (
<img
src={ previewUrl }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
* WordPress dependencies
*/
import { useContext } from '@wordpress/element';
import {
CheckboxControl,
Flex,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { CheckboxControl, Flex } from '@wordpress/components';

/**
* Internal dependencies
*/
import { getFontFaceVariantName } from './utils';
import { FontLibraryContext } from './context';
import FontFaceDemo from './font-demo';
import { unlock } from '../../../lock-unlock';

function LibraryFontVariant( { face, font } ) {
const { isFontActivated, toggleActivateFont } =
Expand All @@ -39,27 +34,23 @@ function LibraryFontVariant( { face, font } ) {
};

const displayName = font.name + ' ' + getFontFaceVariantName( face );
const { kebabCase } = unlock( componentsPrivateApis );
const checkboxId = kebabCase(
`${ font.slug }-${ getFontFaceVariantName( face ) }`
);

return (
<label
className="font-library-modal__library-font-variant"
htmlFor={ checkboxId }
>
<div className="font-library-modal__library-font-variant">
<Flex justify="flex-start" align="center" gap="1rem">
<CheckboxControl
checked={ isInstalled }
onChange={ handleToggleActivation }
__nextHasNoMarginBottom={ true }
id={ checkboxId }
label={ false }
label={ displayName }
/>
<FontFaceDemo
fontFace={ face }
text={ displayName }
onClick={ handleToggleActivation }
/>
<FontFaceDemo fontFace={ face } text={ displayName } />
</Flex>
</label>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
border: 1px solid $gray-200;
padding: $grid-unit-20;
margin-top: -1px; /* To collapse the margin with the previous element */

// Hide the checkbox label while still allowing screen-readers to read it.
// Visual label is part of the `FontFaceDemo` component.
label {
display: inline-block;
width: 0;
height: 0;
overflow: hidden;
}
}

.font-library-modal__font-variant {
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/specs/site-editor/font-library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ test.describe( 'Font Library', () => {
name: /manage fonts/i,
} )
.click();
await page.getByRole( 'button', { name: /system font/i } ).click();
await page
.getByRole( 'button', { name: /system font/i } )
.first()
.click();
await expect(
page.getByRole( 'heading', { name: /system font/i } )
).toBeVisible();
Expand Down
Loading