Repository navigation
Dashboard: per-instance widget settings drawer #78465
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
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c144076
add per-instance widget settings drawer
retrofox b941fe3
add message setting to Hello World widget
retrofox 277cac2
Merge remote-tracking branch 'origin/trunk' into update/widget-settings
Copilot fcf9024
Merge branch 'trunk' into update/widget-settings
retrofox 9245386
Merge branch 'trunk' into update/widget-settings
retrofox cd96b3b
Merge branch 'trunk' into update/widget-settings
retrofox 6201a8f
add WidgetToolbar shell for per-tile actions
retrofox a10f3b0
split per-tile actions into two toolbars
retrofox 8f22439
reveal settings toolbar on tile hover
retrofox efa919c
Merge remote-tracking branch 'origin/update/widget-settings' into upd…
retrofox 73c0c8d
Merge branch 'trunk' into update/widget-settings
retrofox 447114f
Merge branch 'trunk' into update/widget-settings
retrofox 816d141
use a generic label for the settings gear
retrofox 3459320
move admin-bar offset to CSS; skip empty drawer
retrofox 6afdf67
update test for the generic settings gear label
retrofox f584a92
Merge branch 'trunk' into update/widget-settings
retrofox 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
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
routes/dashboard/widget-dashboard/components/widget-settings/index.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,4 @@ | ||
| export { WidgetSettings } from './widget-settings'; | ||
| export { getWidgetSettingsTitle } from './utils'; | ||
| export { WidgetSettingsToolbar } from './widget-settings-toolbar'; | ||
| export type { WidgetSettingsToolbarProps } from './widget-settings-toolbar'; |
30 changes: 30 additions & 0 deletions
30
routes/dashboard/widget-dashboard/components/widget-settings/utils/get-admin-menu-inset.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,30 @@ | ||
| // cspell:ignore adminmenuback | ||
| const ADMIN_MENU_ID = 'adminmenuback'; | ||
|
|
||
| /** | ||
| * Width, in pixels, taken up by the WordPress admin menu | ||
| * (`#adminmenuback`) on the inline-start edge of the viewport. | ||
| * | ||
| * The settings drawer portals to the document body and anchors to a | ||
| * viewport edge, so a left-anchored drawer would slide over the fixed | ||
| * admin menu. Offsetting the drawer by this value keeps the menu | ||
| * uncovered (and reachable) instead of raising the drawer's z-index over | ||
| * it. | ||
| * | ||
| * Defensive: returns `0` when the element is absent (outside wp-admin, or | ||
| * in tests/Storybook), so the drawer just anchors to the viewport edge. | ||
| * | ||
| * @return {number} The admin menu's right edge in px, or `0`. | ||
| */ | ||
| export function getAdminMenuInset(): number { | ||
| if ( typeof document === 'undefined' ) { | ||
| return 0; | ||
| } | ||
|
|
||
| const adminMenu = document.getElementById( ADMIN_MENU_ID ); | ||
| if ( ! adminMenu ) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max( 0, adminMenu.getBoundingClientRect().right ); | ||
| } |
26 changes: 26 additions & 0 deletions
26
routes/dashboard/widget-dashboard/components/widget-settings/utils/get-widget-settings.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,26 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { sprintf, __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import type { WidgetType } from '../../../../widget-types/types'; | ||
|
|
||
| /** | ||
| * Localized "<Widget> settings" label, falling back to a generic title | ||
| * when the type declares none. | ||
| * | ||
| * @param {WidgetType} widgetType Type backing the open instance. | ||
| * @return {string} The drawer/trigger title. | ||
| */ | ||
| export function getWidgetSettingsTitle( widgetType?: WidgetType ): string { | ||
| return widgetType?.title | ||
| ? sprintf( | ||
| /* translators: %s: Widget title. */ | ||
| __( '%s settings' ), | ||
| widgetType.title | ||
| ) | ||
| : __( 'Widget settings' ); | ||
| } |
2 changes: 2 additions & 0 deletions
2
routes/dashboard/widget-dashboard/components/widget-settings/utils/index.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,2 @@ | ||
| export { getAdminMenuInset } from './get-admin-menu-inset'; | ||
| export { getWidgetSettingsTitle } from './get-widget-settings'; |
25 changes: 25 additions & 0 deletions
25
.../dashboard/widget-dashboard/components/widget-settings/widget-settings-toolbar.module.css
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,25 @@ | ||
| /* Translucent surface so the gear stays legible over any widget background. */ | ||
| .widgetSettingsToolbar { | ||
| background: color-mix(in srgb, var(--wpds-color-bg-surface-neutral) 40%, transparent); | ||
| opacity: 0; | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| /* | ||
| * Revealed on tile hover or focus. Hidden with opacity, not display, so the | ||
| * gear stays in the tab order and `:focus-within` surfaces it for keyboard | ||
| * users. The matched ancestor is the grid item, shared by the slot and card. | ||
| */ | ||
| :global([data-wp-grid-item-key]):hover .widgetSettingsToolbar, | ||
| :global([data-wp-grid-item-key]):focus-within .widgetSettingsToolbar { | ||
| opacity: 1; | ||
| pointer-events: auto; | ||
| } | ||
|
|
||
| @media not ( prefers-reduced-motion ) { | ||
| .widgetSettingsToolbar { | ||
| transition: | ||
| opacity var(--wpds-motion-duration-sm) | ||
| var(--wpds-motion-easing-subtle); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
routes/dashboard/widget-dashboard/components/widget-settings/widget-settings-toolbar.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,43 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { WidgetToolbar } from '../widget-toolbar'; | ||
| import { WidgetSettingsTrigger } from './widget-settings-trigger'; | ||
| import styles from './widget-settings-toolbar.module.css'; | ||
| import type { DashboardWidget, WidgetType } from '../../types'; | ||
|
|
||
| export interface WidgetSettingsToolbarProps { | ||
| /** The instance whose settings this toolbar configures. */ | ||
| widget: DashboardWidget< unknown >; | ||
|
|
||
| /** The instance's widget type, for the trigger label and guard. */ | ||
| widgetType: WidgetType; | ||
| } | ||
|
|
||
| /** | ||
| * Normal-mode per-tile toolbar: the gear that opens the settings drawer. | ||
| * Lives in the grid's `actionableArea` slot, so it shows for every | ||
| * `presentation`. Returns `null` when the type has no attributes. | ||
| * | ||
| * @param {WidgetSettingsToolbarProps} props Component props. | ||
| * @param {DashboardWidget} props.widget Instance to edit. | ||
| * @param {WidgetType} props.widgetType Type backing it. | ||
| * @return {React.ReactNode} The settings toolbar, or `null`. | ||
| */ | ||
| export function WidgetSettingsToolbar( { | ||
| widget, | ||
| widgetType, | ||
| }: WidgetSettingsToolbarProps ): React.ReactNode { | ||
| if ( ! widgetType.attributes?.length ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <WidgetToolbar className={ styles.widgetSettingsToolbar }> | ||
| <WidgetSettingsTrigger | ||
| widget={ widget } | ||
| widgetType={ widgetType } | ||
| /> | ||
| </WidgetToolbar> | ||
| ); | ||
| } |
99 changes: 99 additions & 0 deletions
99
routes/dashboard/widget-dashboard/components/widget-settings/widget-settings-trigger.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,99 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useCallback } from '@wordpress/element'; | ||
| import { cog } from '@wordpress/icons'; | ||
| // Dashboard is still experimental. | ||
| // eslint-disable-next-line @wordpress/use-recommended-components | ||
| import { IconButton } from '@wordpress/ui'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { useDashboardUIContext } from '../../context/ui-context'; | ||
| import { getAdminMenuInset, getWidgetSettingsTitle } from './utils'; | ||
| import type { DashboardWidget, WidgetType } from '../../types'; | ||
|
|
||
| export interface WidgetSettingsTriggerProps { | ||
| /** | ||
| * The instance whose settings drawer this gear opens. | ||
| */ | ||
| widget: DashboardWidget< unknown >; | ||
|
|
||
| /** | ||
| * The instance's widget type, used for the accessible label and the | ||
| * attributes guard. | ||
| */ | ||
| widgetType: WidgetType; | ||
| } | ||
|
|
||
| /** | ||
| * Per-instance gear that opens the shared settings drawer for its widget | ||
| * by writing the instance `uuid` to the dashboard UI context. The single | ||
| * `WidgetSettings` drawer at the root reacts to that change. | ||
| * | ||
| * Returns `null` when the type declares no attributes, so chrome can mount | ||
| * it unconditionally. | ||
| * | ||
| * @param {WidgetSettingsTriggerProps} props Component props. | ||
| * @param {DashboardWidget} props.widget Instance to edit. | ||
| * @param {WidgetType} props.widgetType Type backing it. | ||
| * @return {React.ReactNode} The gear button, or `null`. | ||
| */ | ||
| export function WidgetSettingsTrigger( { | ||
| widget, | ||
| widgetType, | ||
| }: WidgetSettingsTriggerProps ): React.ReactNode { | ||
| const { | ||
| setSettingsWidgetUuid, | ||
| setSettingsDrawerSide, | ||
| setSettingsDrawerInset, | ||
| } = useDashboardUIContext(); | ||
|
|
||
| const open = useCallback( | ||
| ( event: React.MouseEvent< HTMLElement > ) => { | ||
| // Open the drawer on the side away from the widget: compare the | ||
| // tile's center against the midpoint of the usable content area | ||
| // (which starts after the admin menu). Past it opens left. | ||
| const adminMenuInset = getAdminMenuInset(); | ||
| // The gear sits in the grid slot, outside the card, so reach the | ||
| // tile via the grid item's data hook. | ||
| const tile = event.currentTarget.closest( | ||
| '[data-wp-grid-item-key]' | ||
| ); | ||
| const rect = ( | ||
| tile ?? event.currentTarget | ||
| ).getBoundingClientRect(); | ||
| const widgetCenter = rect.left + rect.width / 2; | ||
| const contentCenter = ( adminMenuInset + window.innerWidth ) / 2; | ||
| const side = widgetCenter > contentCenter ? 'left' : 'right'; | ||
|
|
||
| setSettingsDrawerSide( side ); | ||
| // A left drawer would otherwise slide over the fixed admin menu; | ||
| // offset it by the menu width so it lands clear of it. | ||
| setSettingsDrawerInset( side === 'left' ? adminMenuInset : 0 ); | ||
| setSettingsWidgetUuid( widget.uuid ); | ||
| }, | ||
| [ | ||
| setSettingsDrawerSide, | ||
| setSettingsDrawerInset, | ||
| setSettingsWidgetUuid, | ||
| widget.uuid, | ||
| ] | ||
| ); | ||
|
|
||
| if ( ! widgetType.attributes?.length ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <IconButton | ||
| icon={ cog } | ||
| label={ getWidgetSettingsTitle( widgetType ) } | ||
| variant="minimal" | ||
| tone="neutral" | ||
| size="compact" | ||
| onClick={ open } | ||
| /> | ||
| ); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just "Settings" or "Widget settings" might work well enough here too, without the widget name.