-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add support for Synced Patterns #173
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
Open
omrjadhav
wants to merge
6
commits into
rtCamp:develop
Choose a base branch
from
omrjadhav:update-aria-label
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20dd925
Fix: Add aria-label to CoreButton component for accessibility
2199938
Fix: Add aria-label to CoreButton component for accessibility
b742015
chore: `npm run format`
justlevine 9e8aaa0
chore: use CLI for changeset generation
justlevine 6d8e042
chore: fix imports and type errors
justlevine da6501e
Merge branch 'develop' into update-aria-label
justlevine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@snapwp/blocks": patch | ||
| "@snapwp/types": patch | ||
| --- | ||
|
|
||
| feat: Add support for overloading Synced Pattern blocks. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| cn, | ||
| getClassNamesFromString, | ||
| getStylesFromAttributes, | ||
| } from '@snapwp/core'; | ||
| import type { | ||
| CoreSyncedPattern as CoreSyncedPatternType, | ||
| CoreSyncedPatternProps, | ||
| } from '@snapwp/types'; | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| /** | ||
| * Renders the core/synced-pattern block. | ||
| * | ||
| * @param {Object} props The props for the block component. | ||
| * @param {CoreSyncedPatternProps['attributes']} props.attributes Block attributes. | ||
| * @param {ReactNode} props.children The block's children. | ||
| * @param {CoreSyncedPatternProps['renderedHtml']} props.renderedHtml The block's rendered HTML. | ||
| * | ||
| * @return The rendered block. | ||
| */ | ||
| export const CoreSyncedPattern: CoreSyncedPatternType = ( { | ||
| attributes, | ||
| children, | ||
| renderedHtml, | ||
| }: CoreSyncedPatternProps ): ReactNode => { | ||
| const { style } = attributes || {}; | ||
| const styleObject = getStylesFromAttributes( { style } ); | ||
|
|
||
| /** | ||
| * @todo replace with cssClassName once it's supported. | ||
| */ | ||
| const classNamesFromString = renderedHtml | ||
| ? getClassNamesFromString( renderedHtml ) | ||
| : ''; | ||
| const classNames = cn( classNamesFromString ); | ||
|
|
||
| return ( | ||
| <div | ||
| className={ classNames } | ||
| { ...( styleObject && { style: styleObject } ) } | ||
| > | ||
| { children } | ||
| </div> | ||
| ); | ||
| }; | ||
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
81 changes: 81 additions & 0 deletions
81
packages/blocks/src/blocks/tests/core-synced-pattern.test.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,81 @@ | ||
| import { render } from '@testing-library/react'; | ||
|
|
||
| import { CoreSyncedPattern } from '../core-synced-pattern'; | ||
|
|
||
| describe( 'CoreSyncedPattern Component', () => { | ||
| it( 'renders children correctly', () => { | ||
| const { container } = render( | ||
| <CoreSyncedPattern> | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| expect( container ).toHaveTextContent( 'Test Child' ); | ||
| } ); | ||
|
|
||
| it( 'applies className from renderedHtml', () => { | ||
| const renderedHtml = '<div class="test-class another-class"></div>'; | ||
| const { container } = render( | ||
| <CoreSyncedPattern renderedHtml={ renderedHtml }> | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| const wrapper = container.firstChild; | ||
| expect( wrapper ).toHaveClass( 'test-class', 'another-class' ); | ||
| } ); | ||
|
|
||
| it( 'applies style from attributes', () => { | ||
| const style = JSON.stringify( { color: 'red', padding: '10px' } ); | ||
| const { container } = render( | ||
| <CoreSyncedPattern attributes={ { style } }> | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| const wrapper = container.firstChild; | ||
| expect( wrapper ).toHaveStyle( { | ||
| color: 'red', | ||
| padding: '10px', | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'handles empty attributes gracefully', () => { | ||
| const { container } = render( | ||
| //@ts-ignore to test undefined props | ||
| <CoreSyncedPattern attributes={ undefined }> | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| expect( container ).toHaveTextContent( 'Test Child' ); | ||
| } ); | ||
|
|
||
| it( 'handles empty renderedHtml gracefully', () => { | ||
| const { container } = render( | ||
| // @ts-ignore to test undefined props | ||
| <CoreSyncedPattern renderedHtml=""> | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| expect( container ).toHaveTextContent( 'Test Child' ); | ||
| } ); | ||
|
|
||
| it( 'combines className and style correctly', () => { | ||
| const renderedHtml = '<div class="test-class"></div>'; | ||
| const style = JSON.stringify( { color: 'blue' } ); | ||
| const { container } = render( | ||
| <CoreSyncedPattern | ||
| renderedHtml={ renderedHtml } | ||
| attributes={ { style } } | ||
| > | ||
| <div>Test Child</div> | ||
| </CoreSyncedPattern> | ||
| ); | ||
|
|
||
| const wrapper = container.firstChild; | ||
| expect( wrapper ).toHaveClass( 'test-class' ); | ||
| expect( wrapper ).toHaveStyle( { color: 'blue' } ); | ||
| } ); | ||
| } ); |
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,10 @@ | ||
| import type { BaseProps } from '../base'; | ||
| import type { ComponentType, PropsWithChildren } from 'react'; | ||
|
|
||
| export type CoreSyncedPatternProps = PropsWithChildren< | ||
| BaseProps< { | ||
| style?: string; | ||
| } > | ||
| >; | ||
|
|
||
| export type CoreSyncedPattern = ComponentType< CoreSyncedPatternProps >; |
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
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.
The 'style' attribute is defined as a string in the type definitions but later processed via getStylesFromAttributes. Consider clarifying the expected format and ensuring consistency with usage examples and documentation.