Skip to content
Merged
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
4 changes: 4 additions & 0 deletions projects/plugins/protect/changelog/update-protect-notice-wpds
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Notices: Restyle to match the WordPress design system.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ A simple notice component for displaying alerts and messages to the user.

## Props

Supported `type` values are `info`, `success`, and `error`.
Supported `type` values are `info`, `success`, `warning`, and `error`.
53 changes: 24 additions & 29 deletions projects/plugins/protect/src/js/components/notice/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { __ } from '@wordpress/i18n';
import { check, close, info, cautionFilled as warning, Icon } from '@wordpress/icons';
import { Notice as WPNotice } from '@wordpress/ui';
import clsx from 'clsx';
import { useCallback, useEffect } from 'react';
import useNotices from '../../hooks/use-notices';
import styles from './styles.module.scss';

const INTENTS = {
success: 'success',
error: 'error',
info: 'info',
warning: 'warning',
};

const Notice = ( {
dismissable = false,
duration = null,
Expand All @@ -13,19 +21,6 @@ const Notice = ( {
} ) => {
const { clearNotice } = useNotices();

let icon;
switch ( type ) {
case 'success':
icon = check;
break;
case 'error':
icon = warning;
break;
case 'info':
default:
icon = info;
}

const onClose = useCallback( () => {
clearNotice();
}, [ clearNotice ] );
Expand All @@ -44,25 +39,25 @@ const Notice = ( {
}, [ clearNotice, duration, message ] );

return (
<div
className={ `${ styles.notice } ${ styles[ `notice--${ type }` ] } ${
floating ? styles[ 'notice--floating' ] : ''
}` }
<WPNotice.Root
intent={ INTENTS[ type ] || 'info' }
className={ clsx(
styles.notice,
styles[ `notice--${ type }` ],
floating && styles[ 'notice--floating' ]
) }
// `Notice.Root` serializes `spokenMessage` mid-render, so a JSX message runs its
// hooks inside Root and corrupts hook order. Strings only, and only for the toast.
spokenMessage={ floating && 'string' === typeof message ? message : null }
>
<div className={ styles.notice__icon }>
<Icon icon={ icon } />
</div>
<div className={ styles.notice__message }>{ message }</div>
<WPNotice.Description>{ message }</WPNotice.Description>
{ dismissable && (
<button
className={ styles.notice__close }
aria-label={ __( 'Dismiss notice.', 'jetpack-protect' ) }
<WPNotice.CloseIcon
label={ __( 'Dismiss notice.', 'jetpack-protect' ) }
onClick={ onClose }
>
<Icon icon={ close } />
</button>
/>
) }
</div>
</WPNotice.Root>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
.notice {
background-color: var(--jp-gray-90);
color: var(--jp-white);
display: flex;
border-radius: var(--jp-border-radius); // 4px
overflow: hidden;
z-index: 1;

&.notice--info {
border-left: 4px solid var(--jp-yellow-20);
margin-bottom: calc(var(--spacing-base) * 3); // 24px
}

Expand All @@ -17,59 +11,12 @@
right: calc(var(--spacing-base) * 3); // 24px
margin-left: calc(var(--spacing-base) * 3); // 24px

// The notice sets `container-type: inline-size`, so it reports no width to
// this shrink-to-fit fixed box and collapses to a sliver.
container-type: normal;

@media ( max-width: 782px ) {
top: calc(var(--spacing-base) * 8); // 72px
}
}

a,
a:link,
a:hover,
a:visited,
a:active {
color: var(--jp-white);
}
}

.notice__icon {
background-color: var(--jp-yellow-30);
fill: var(--jp-white);
display: flex;
align-items: center;
justify-content: center;
padding: calc(var(--spacing-base) * 1.5); // 12px

.notice--success & {
background-color: var(--jp-green-50);
}

.notice--error & {
background-color: var(--jp-red);
}

.notice--info & {
fill: var(--jp-yellow-50);
background-color: var(--jp-yellow-5);
}
}

.notice__message {
font-size: 14px;
padding: calc(var(--spacing-base) * 1.75); // 14px

.notice--info & {
color: var(--jp-gray-90);
background-color: var(--jp-yellow-5);
}
}

.notice__close {
fill: var(--jp-gray);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
padding: calc(var(--spacing-base) * 1.5); // 12px
background: transparent;
border: none;
}
2 changes: 1 addition & 1 deletion projects/plugins/protect/src/js/hooks/use-notices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface NoticeState {
message?: string | JSX.Element;
dismissable?: boolean;
duration?: number;
type?: 'success' | 'info' | 'error';
type?: 'success' | 'info' | 'warning' | 'error';
}

interface NoticeContextValue {
Expand Down
10 changes: 7 additions & 3 deletions projects/plugins/protect/tests/e2e/specs/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type { Locator, Page } from '@playwright/test';
* @param {Page} page - Playwright page object
*/
async function closeChangesSavedNotice( page: Page ) {
await expect( page.getByText( 'Changes saved' ) ).toBeVisible();
// Scoped to the app root: the toast announces, and `speak()` leaves its text in
// `#a11y-speak-polite` after the notice goes, which would match on later calls.
await expect(
page.locator( '#jetpack-protect-root' ).getByText( 'Changes saved' )
).toBeVisible();
await page.getByRole( 'button', { name: 'Dismiss notice.' } ).click();
}

Expand Down Expand Up @@ -69,8 +73,8 @@ test.describe( 'Jetpack Protect Plugin', () => {
// react-router <Outlet />, the matched FirewallRoute can appear in two panels at
// once. Scoping to the Firewall panel keeps each locator matching a single
// element instead of tripping Playwright strict mode. `exact` avoids matching the
// "Automatic firewall is on" heading variant. The "Changes saved" notice is left
// page-scoped on purpose: it renders at the app level, outside the tab panels.
// "Automatic firewall is on" heading variant. The "Changes saved" notice renders
// at the app level, so its helper scopes to the app root rather than a panel.
const firewallPanel = page.getByRole( 'tabpanel', { name: 'Firewall', exact: true } );

await test.step( 'Navigate to firewall page', async () => {
Expand Down
12 changes: 11 additions & 1 deletion projects/plugins/protect/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ module.exports = [
...jetpackWebpackConfig.resolve,
},
node: false,
plugins: [ ...jetpackWebpackConfig.StandardPlugins() ],
plugins: [
...jetpackWebpackConfig.StandardPlugins( {
DependencyExtractionPlugin: {
// Bundled, never one without the other — see #48173.
requestMap: {
'@wordpress/theme': { external: false },
'@wordpress/private-apis': { external: false },
},
},
} ),
],
module: {
strictExportPresence: true,
rules: [
Expand Down
Loading