Repository navigation
Components: Move Newsletter's placement card to @automattic/jetpack-components as CheckboxCard #53733
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
Merged
Merged
Components: Move Newsletter's placement card to @automattic/jetpack-components as CheckboxCard #53733
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
978b800
Components: Move Newsletter's PlacementCard to jetpack-components
jeherve 188c2ee
Components: Describe PlacementCard's own contract in its preview link…
jeherve 4ebae43
Components: Drop PlacementCard's rule for a checkbox label that never…
jeherve ea9e875
Merge remote-tracking branch 'origin/trunk' into update/placement-car…
jeherve c61e4ea
Merge remote-tracking branch 'origin/trunk' into update/placement-car…
jeherve af7a007
Components: Build PlacementCard from @wordpress/ui primitives
jeherve f6302e3
ESLint: Drop the deleted Newsletter placement-card.tsx from the exclu…
jeherve ec6b333
Components: Rename PlacementCard to CheckboxCard
jeherve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/js-packages/components/changelog/update-placement-card-shared-component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Add a CheckboxCard component. |
79 changes: 79 additions & 0 deletions
79
projects/js-packages/components/components/checkbox-card/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { useCallback } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Card, Checkbox, Field, Link, Stack, Text } from '@wordpress/ui'; | ||
| import clsx from 'clsx'; | ||
| import './style.scss'; | ||
| import type { CheckboxCardGridProps, CheckboxCardProps } from './types.ts'; | ||
| import type { JSX } from 'react'; | ||
|
|
||
| /** | ||
| * Lays out checkbox cards in two columns, or one on narrow screens. | ||
| * | ||
| * @param {CheckboxCardGridProps} props - Component props. | ||
| * @return {JSX.Element} The grid. | ||
| */ | ||
| export function CheckboxCardGrid( { children, className }: CheckboxCardGridProps ): JSX.Element { | ||
| return <div className={ clsx( 'jp-checkbox-card-grid', className ) }>{ children }</div>; | ||
| } | ||
|
|
||
| /** | ||
| * A card with a checkbox, toggled by a click on its illustration. | ||
| * | ||
| * @param {CheckboxCardProps} props - Component props. | ||
| * @return {JSX.Element} The card. | ||
| */ | ||
| export default function CheckboxCard( { | ||
| id, | ||
| name, | ||
| title, | ||
| illustration, | ||
| checked, | ||
| onChange, | ||
| previewUrl, | ||
| onPreviewClick, | ||
| disabled, | ||
| }: CheckboxCardProps ): JSX.Element { | ||
| const titleId = `${ id }__title`; | ||
|
|
||
| const handleChange = useCallback( | ||
| ( next: boolean ) => onChange( name, next ), | ||
| [ name, onChange ] | ||
| ); | ||
|
|
||
| const handlePreviewClick = useCallback( () => { | ||
| onPreviewClick?.( name ); | ||
| }, [ name, onPreviewClick ] ); | ||
|
|
||
| return ( | ||
| <Field.Root disabled={ disabled }> | ||
| <Card.Root className="jp-checkbox-card" data-checked={ checked }> | ||
| <Card.Content> | ||
| <Card.FullBleed className="jp-checkbox-card__surface"> | ||
| { /* Only the illustration toggles: the title and link sit outside the label. */ } | ||
| <Field.Label className="jp-checkbox-card__illustration">{ illustration }</Field.Label> | ||
| <Checkbox | ||
| id={ id } | ||
| className="jp-checkbox-card__checkbox" | ||
| checked={ checked } | ||
| onCheckedChange={ handleChange } | ||
| aria-labelledby={ titleId } | ||
| /> | ||
| </Card.FullBleed> | ||
| </Card.Content> | ||
| </Card.Root> | ||
| <Stack gap="xs" direction="column"> | ||
| <Text id={ titleId } variant="body-sm"> | ||
| { title } | ||
| </Text> | ||
| { previewUrl && ( | ||
| <Link href={ previewUrl } onClick={ handlePreviewClick }> | ||
| { __( 'Preview and edit', 'jetpack-components' ) } | ||
| </Link> | ||
| ) } | ||
| </Stack> | ||
| </Field.Root> | ||
| ); | ||
| } | ||
|
|
||
| export { CheckboxCard }; | ||
| export type { CheckboxCardProps, CheckboxCardGridProps }; |
90 changes: 90 additions & 0 deletions
90
projects/js-packages/components/components/checkbox-card/stories/index.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { useCallback, useState } from 'react'; | ||
| import CheckboxCard, { CheckboxCardGrid } from '../index.tsx'; | ||
| import type { Meta, StoryFn } from '@storybook/react'; | ||
|
|
||
| const meta: Meta< typeof CheckboxCard > = { | ||
| title: 'JS Packages/Components/Checkbox Card', | ||
| component: CheckboxCard, | ||
| argTypes: { | ||
| illustration: { table: { disable: true } }, | ||
| onChange: { table: { disable: true } }, | ||
| onPreviewClick: { table: { disable: true } }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| const Wireframe = ( { before }: { before?: boolean } ) => { | ||
| const top = before ? 56 : 24; | ||
| return ( | ||
| <svg viewBox="0 0 294 192" aria-hidden="true"> | ||
| <rect x="40" y={ top } width="214" height="12" rx="2" fill="#dcdcde" /> | ||
| <rect x="40" y={ top + 24 } width="214" height="80" rx="2" fill="#f0f0f1" /> | ||
| <rect x="40" y={ before ? 24 : 140 } width="120" height="20" rx="4" fill="#3858e9" /> | ||
| </svg> | ||
| ); | ||
| }; | ||
|
|
||
| const Template: StoryFn< typeof CheckboxCard > = args => { | ||
| const [ checked, setChecked ] = useState( args.checked ); | ||
| const handleChange = useCallback( ( _name: string, next: boolean ) => setChecked( next ), [] ); | ||
|
|
||
| return ( | ||
| <div style={ { maxWidth: 360 } }> | ||
| <CheckboxCard { ...args } checked={ checked } onChange={ handleChange } /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const _default = Template.bind( {} ); | ||
| _default.args = { | ||
| id: 'placement-after-content', | ||
| name: 'after_content', | ||
| title: 'After the post content', | ||
| illustration: <Wireframe />, | ||
| checked: false, | ||
| }; | ||
|
|
||
| export const WithPreviewLink = Template.bind( {} ); | ||
| WithPreviewLink.args = { | ||
| ..._default.args, | ||
| checked: true, | ||
| previewUrl: '#', | ||
| }; | ||
|
|
||
| export const Disabled = Template.bind( {} ); | ||
| Disabled.args = { | ||
| ..._default.args, | ||
| disabled: true, | ||
| }; | ||
|
|
||
| export const Grid: StoryFn< typeof CheckboxCard > = () => { | ||
| const [ selected, setSelected ] = useState< Record< string, boolean > >( { | ||
| before_content: true, | ||
| } ); | ||
| const handleChange = useCallback( | ||
| ( name: string, next: boolean ) => setSelected( prev => ( { ...prev, [ name ]: next } ) ), | ||
| [] | ||
| ); | ||
|
|
||
| return ( | ||
| <CheckboxCardGrid> | ||
| <CheckboxCard | ||
| id="placement-before-content" | ||
| name="before_content" | ||
| title="Before the post content" | ||
| illustration={ <Wireframe before /> } | ||
| checked={ Boolean( selected.before_content ) } | ||
| onChange={ handleChange } | ||
| /> | ||
| <CheckboxCard | ||
| id="placement-after-content" | ||
| name="after_content" | ||
| title="After the post content" | ||
| illustration={ <Wireframe /> } | ||
| checked={ Boolean( selected.after_content ) } | ||
| onChange={ handleChange } | ||
| /> | ||
| </CheckboxCardGrid> | ||
| ); | ||
| }; |
54 changes: 54 additions & 0 deletions
54
projects/js-packages/components/components/checkbox-card/style.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| @use "@automattic/jetpack-base-styles/gutenberg-base-styles" as gb; | ||
|
|
||
| .jp-checkbox-card-grid { | ||
| display: grid; | ||
| grid-template-columns: repeat(2, minmax(0, 1fr)); | ||
| column-gap: var(--wpds-dimension-gap-md); | ||
| row-gap: var(--wpds-dimension-gap-2xl); | ||
|
|
||
| @media (max-width: 720px) { | ||
| grid-template-columns: minmax(0, 1fr); | ||
| } | ||
| } | ||
|
|
||
| .jp-checkbox-card { | ||
|
|
||
| &[data-checked="true"] { | ||
| border-color: var(--wpds-color-stroke-interactive-brand-active); | ||
| } | ||
|
|
||
| &:hover:not([data-checked="true"]) { | ||
| border-color: var(--wpds-color-stroke-surface-neutral-strong); | ||
| } | ||
|
|
||
| &:has(:focus-visible) { | ||
|
|
||
| @include gb.outset-ring__focus(); | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: no-preference) { | ||
| transition: border-color 0.15s ease-out; | ||
| } | ||
| } | ||
|
|
||
| .jp-checkbox-card__surface { | ||
| position: relative; | ||
| } | ||
|
|
||
| .jp-checkbox-card__illustration { | ||
| display: flex; | ||
| aspect-ratio: 294 / 192; | ||
| background: var(--wpds-color-background-surface-neutral-weak); | ||
| cursor: var(--wpds-cursor-control); | ||
|
|
||
| > svg { | ||
| inline-size: 100%; | ||
| block-size: 100%; | ||
| } | ||
| } | ||
|
|
||
| .jp-checkbox-card__checkbox { | ||
| position: absolute; | ||
| inset-block-start: var(--wpds-dimension-padding-md); | ||
| inset-inline-start: var(--wpds-dimension-padding-md); | ||
| } | ||
73 changes: 73 additions & 0 deletions
73
projects/js-packages/components/components/checkbox-card/test/component.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { jest } from '@jest/globals'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { userEvent } from '@testing-library/user-event'; | ||
| import CheckboxCard from '../index.tsx'; | ||
| import type { CheckboxCardProps } from '../types.ts'; | ||
|
|
||
| const renderCard = ( props: Partial< CheckboxCardProps > = {} ) => { | ||
| const onChange = jest.fn(); | ||
| const onPreviewClick = jest.fn(); | ||
| render( | ||
| <CheckboxCard | ||
| id="placement-after-content" | ||
| name="after_content" | ||
| title="After the post content" | ||
| illustration={ <span>Wireframe</span> } | ||
| checked={ false } | ||
| onChange={ onChange } | ||
| onPreviewClick={ onPreviewClick } | ||
| { ...props } | ||
| /> | ||
| ); | ||
| return { onChange, onPreviewClick }; | ||
| }; | ||
|
|
||
| describe( 'CheckboxCard', () => { | ||
| // Catches the title id and `aria-labelledby` drifting apart, which leaves the checkbox unnamed. | ||
| it( 'names the checkbox with the title', () => { | ||
| renderCard( { checked: true } ); | ||
|
|
||
| expect( screen.getByRole( 'checkbox', { name: 'After the post content' } ) ).toBeChecked(); | ||
| } ); | ||
|
|
||
| // Catches the label's `htmlFor` no longer matching the checkbox, or `onChange` losing the name. | ||
| it.each( [ | ||
| [ 'ticks', false, true ], | ||
| [ 'unticks', true, false ], | ||
| ] )( '%s the card when the illustration is clicked', async ( _, checked, next ) => { | ||
| const { onChange } = renderCard( { checked } ); | ||
|
|
||
| await userEvent.click( screen.getByText( 'Wireframe' ) ); | ||
|
|
||
| expect( onChange ).toHaveBeenCalledWith( 'after_content', next ); | ||
| } ); | ||
|
|
||
| // Catches the title moving inside the label, where a click meant for the link toggles the card. | ||
| it( 'does not toggle when the title is clicked', async () => { | ||
| const { onChange } = renderCard(); | ||
|
|
||
| await userEvent.click( screen.getByText( 'After the post content' ) ); | ||
|
|
||
| expect( onChange ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| // Catches the link rendering without a `previewUrl`, or going missing when one is passed. | ||
| it.each( [ | ||
| [ 'shows', 'https://example.com/wp-admin/site-editor.php', 1 ], | ||
| [ 'hides', undefined, 0 ], | ||
| ] )( '%s the preview link', ( _, previewUrl, count ) => { | ||
| renderCard( { previewUrl } ); | ||
|
|
||
| // By text: without an `href` the anchor has no link role, so a role query would miss it. | ||
| expect( screen.queryAllByText( 'Preview and edit' ) ).toHaveLength( count ); | ||
| } ); | ||
|
|
||
| // Catches the preview handler losing the name the parent uses to tell cards apart. | ||
| it( 'passes its name to the preview handler', async () => { | ||
| const { onPreviewClick } = renderCard( { previewUrl: '#preview' } ); | ||
|
|
||
| await userEvent.click( screen.getByRole( 'link', { name: 'Preview and edit' } ) ); | ||
|
|
||
| expect( onPreviewClick ).toHaveBeenCalledWith( 'after_content' ); | ||
| } ); | ||
| } ); |
51 changes: 51 additions & 0 deletions
51
projects/js-packages/components/components/checkbox-card/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| export type CheckboxCardProps = { | ||
| /** | ||
| * Id of the checkbox; the title's id is built from it. | ||
| */ | ||
| id: string; | ||
| /** | ||
| * Passed back to `onChange` and `onPreviewClick`, so one handler can serve every card. | ||
| */ | ||
| name: string; | ||
| /** | ||
| * Caption under the card, and the checkbox's accessible name. | ||
| */ | ||
| title: string; | ||
| /** | ||
| * Picture filling the card, drawn at a 294×192 aspect ratio. | ||
| */ | ||
| illustration?: ReactNode; | ||
| /** | ||
| * Whether the card is checked. | ||
| */ | ||
| checked: boolean; | ||
| /** | ||
| * Called with the card's `name` and the next checked value. | ||
| */ | ||
| onChange: ( name: string, next: boolean ) => void; | ||
| /** | ||
| * Shows a "Preview and edit" link to this URL under the title. | ||
| */ | ||
| previewUrl?: string; | ||
| /** | ||
| * Called with the card's `name` when the "Preview and edit" link is clicked. | ||
| */ | ||
| onPreviewClick?: ( name: string ) => void; | ||
| /** | ||
| * Disables the checkbox. | ||
| */ | ||
| disabled?: boolean; | ||
| }; | ||
|
|
||
| export type CheckboxCardGridProps = { | ||
| /** | ||
| * The `CheckboxCard`s to lay out. | ||
| */ | ||
| children: ReactNode; | ||
| /** | ||
| * Extra class for the grid. | ||
| */ | ||
| className?: string; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/newsletter/changelog/update-placement-card-shared-component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Settings: Use the shared CheckboxCard component for subscription placements. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.