-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add block visibility breakpoints support for dynamic configuration #73735
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
Draft
ramonjd
wants to merge
24
commits into
trunk
Choose a base branch
from
try/hide-blocks-breakpoints
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
fc82266
Add block visibility breakpoints support for dynamic configuration
ramonjd 90c3248
Combine menu
ramonjd a2b012e
Refactor block visibility breakpoints: remove menu item, streamline t…
ramonjd ab4d101
Enhance block visibility breakpoints handling by adding a check for t…
ramonjd c459385
Refactor block visibility breakpoints modal to ensure that visibility…
ramonjd 2f8d4e0
Enhance block visibility logic by adding checks for block visibility …
ramonjd d0c8eb6
Add block visibility breakpoints constants and enhance modal function…
ramonjd 15e8869
Remove block visibility breakpoints support and enhance block visibil…
ramonjd 157c45d
Refactored block visibility checks to use a common function, reducing…
ramonjd 1bf3c23
Update block visibility logic to use strict boolean checks for breakp…
ramonjd f7c8af8
Refactor block visibility components to integrate modal functionality…
ramonjd 5ae1a8f
Update block visibility breakpoints modal text and improve z-index ha…
ramonjd a504192
Remove block visibility breakpoints documentation file. This deletion…
ramonjd e1db740
Refactor block-list styles to consolidate hidden block visibility rul…
ramonjd 386fcf1
Added useRef and useEffect hooks to manage the visibility button's s…
ramonjd f6ed9b8
Integrate success notices in block visibility breakpoints modal. Adde…
ramonjd 68bc072
e2e tests for block visibility breakpoints
ramonjd 5c33808
Using WP components
ramonjd bd6e84a
Refactor block visibility logic in useBlockProps to improve clarity a…
ramonjd 2004e96
Extract logic for preview breakpoints to a separate hook.
ramonjd 386c102
Refactor block visibility logic in isBlockHidden selector to simplify…
ramonjd 0626bff
Enhance block visibility breakpoints modal with conditional rendering…
ramonjd b9ae19e
List view icon will now show if the block is hidden everywhere or has…
ramonjd 27cc0c7
Add device type handling to block visibility logic
ramonjd 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
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
106 changes: 106 additions & 0 deletions
106
packages/block-editor/src/components/block-list/use-block-props/use-preview-breakpoint.js
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,106 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useViewportMatch } from '@wordpress/compose'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { useMemo } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../../store'; | ||
| import { deviceTypeKey } from '../../../store/private-keys'; | ||
|
|
||
| /** | ||
| * Detects the current viewport breakpoint and returns visibility classes for blocks. | ||
| * | ||
| * When deviceType is 'Desktop', uses actual viewport detection via useViewportMatch. | ||
| * When deviceType is 'Mobile' or 'Tablet', overrides viewport detection with the device type. | ||
| * | ||
| * This hook: | ||
| * 1. Gets block visibility settings from block attributes | ||
| * 2. Gets device type from block editor settings | ||
| * 3. Detects viewport (either from deviceType override or actual viewport) | ||
| * 4. Returns the appropriate CSS classes and hidden state | ||
| * | ||
| * @param {string} clientId Block client ID. | ||
| * @return {Object} Visibility classes and state with `breakpointClasses` (Object) and `isHiddenEverywhere` (boolean). | ||
| */ | ||
| export function usePreviewBreakpoint( clientId ) { | ||
| // Get visibility settings from block attributes and device type from settings | ||
| const { blockVisibility, breakpointVisibility, deviceType } = useSelect( | ||
| ( select ) => { | ||
| const block = select( blockEditorStore ).getBlock( clientId ); | ||
| const metadata = block?.attributes?.metadata; | ||
| const settings = select( blockEditorStore ).getSettings(); | ||
| return { | ||
| blockVisibility: metadata?.blockVisibility, | ||
| breakpointVisibility: metadata?.blockVisibilityBreakpoints, | ||
| deviceType: settings?.[ deviceTypeKey ] || 'Desktop', | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| // When Desktop is selected, use actual viewport detection | ||
| // When Mobile/Tablet is selected, override with device type | ||
| // All hooks must be called unconditionally | ||
| const isSmallOrLarger = useViewportMatch( 'small', '>=' ); // >= 600px | ||
| const isLargeOrLarger = useViewportMatch( 'large', '>=' ); // >= 960px | ||
|
|
||
| // Determine viewport flags based on deviceType | ||
| let isMobileViewport, isTabletViewport, isDesktopViewport; | ||
|
|
||
| if ( deviceType === 'Mobile' ) { | ||
| // Override: force mobile viewport | ||
| isMobileViewport = true; | ||
| isTabletViewport = false; | ||
| isDesktopViewport = false; | ||
| } else if ( deviceType === 'Tablet' ) { | ||
| // Override: force tablet viewport | ||
| isMobileViewport = false; | ||
| isTabletViewport = true; | ||
| isDesktopViewport = false; | ||
| } else { | ||
| // Desktop: use actual viewport detection | ||
| // Mobile: viewport < 600px (matches PHP: max-width: 599px) | ||
| isMobileViewport = ! isSmallOrLarger; | ||
| // Tablet: viewport >= 600px and < 960px (matches PHP: 600px-959px) | ||
| isTabletViewport = isSmallOrLarger && ! isLargeOrLarger; | ||
| // Desktop: viewport >= 960px (matches PHP: min-width: 960px) | ||
| isDesktopViewport = isLargeOrLarger; | ||
| } | ||
|
|
||
| // Only apply is-block-hidden class if hidden everywhere (not for breakpoint visibility) | ||
| // Breakpoint visibility is handled by specific classes below | ||
| const isHiddenEverywhere = blockVisibility === false; | ||
|
|
||
| // Memoize breakpoint classes to avoid recreating object on every render | ||
| const breakpointClasses = useMemo( () => { | ||
| if ( ! breakpointVisibility ) { | ||
| return {}; | ||
| } | ||
| return { | ||
| 'wp-block-hidden-mobile': | ||
| breakpointVisibility.mobile && isMobileViewport, | ||
| 'wp-block-hidden-tablet': | ||
| breakpointVisibility.tablet && isTabletViewport, | ||
| 'wp-block-hidden-desktop': | ||
| breakpointVisibility.desktop && isDesktopViewport, | ||
| }; | ||
| }, [ | ||
| breakpointVisibility, | ||
| isMobileViewport, | ||
| isTabletViewport, | ||
| isDesktopViewport, | ||
| ] ); | ||
|
|
||
| // Memoize return object to maintain referential equality | ||
| return useMemo( | ||
| () => ( { | ||
| breakpointClasses, | ||
| isHiddenEverywhere, | ||
| } ), | ||
| [ breakpointClasses, isHiddenEverywhere ] | ||
| ); | ||
| } |
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
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.
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.
This would use the style engine enqueuing system eventually, just a shortcut for now