-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separator block: refactor to use color block supports to allow alpha …
…channel opacity to be set(#38428) Co-authored-by: Glen Davies <[email protected]> Co-authored-by: ramonjd <[email protected]>
- Loading branch information
1 parent
3d07bf6
commit c9e1159
Showing
38 changed files
with
455 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getColorClassName, useBlockProps } from '@wordpress/block-editor'; | ||
|
||
const v1 = { | ||
attributes: { | ||
color: { | ||
type: 'string', | ||
}, | ||
customColor: { | ||
type: 'string', | ||
}, | ||
}, | ||
save( { attributes } ) { | ||
const { color, customColor } = attributes; | ||
|
||
// the hr support changing color using border-color, since border-color | ||
// is not yet supported in the color palette, we use background-color | ||
const backgroundClass = getColorClassName( 'background-color', color ); | ||
// the dots styles uses text for the dots, to change those dots color is | ||
// using color, not backgroundColor | ||
const colorClass = getColorClassName( 'color', color ); | ||
|
||
const className = classnames( { | ||
'has-text-color has-background': color || customColor, | ||
[ backgroundClass ]: backgroundClass, | ||
[ colorClass ]: colorClass, | ||
} ); | ||
|
||
const style = { | ||
backgroundColor: backgroundClass ? undefined : customColor, | ||
color: colorClass ? undefined : customColor, | ||
}; | ||
|
||
return <hr { ...useBlockProps.save( { className, style } ) } />; | ||
}, | ||
migrate( attributes ) { | ||
const { color, customColor } = attributes; | ||
return { | ||
...omit( attributes, [ 'color', 'customColor' ] ), | ||
backgroundColor: color ? color : undefined, | ||
opacity: 'css', | ||
style: customColor | ||
? { color: { background: customColor } } | ||
: undefined, | ||
}; | ||
}, | ||
}; | ||
|
||
export default [ v1 ]; |
This file contains 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 @@ | ||
.wp-block-separator { | ||
// V1 version of the block expects a default opactiy of 0.4 to be set. | ||
&.has-css-opacity { | ||
opacity: 0.4; | ||
} | ||
} |
This file contains 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 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 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 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
24 changes: 0 additions & 24 deletions
24
packages/block-library/src/separator/separator-settings.js
This file was deleted.
Oops, something went wrong.
This file contains 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,113 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SeparatorEdit from '../edit'; | ||
|
||
jest.mock( '@wordpress/block-editor', () => ( { | ||
...jest.requireActual( '@wordpress/block-editor' ), | ||
useBlockProps: jest.fn(), | ||
} ) ); | ||
|
||
const defaultAttributes = { | ||
backgroundColor: undefined, | ||
opacity: 'alpha-channel', | ||
style: {}, | ||
className: '', | ||
}; | ||
const defaultProps = { | ||
attributes: defaultAttributes, | ||
setAttributes: jest.fn(), | ||
}; | ||
|
||
describe( 'Separator block edit method', () => { | ||
beforeEach( () => { | ||
useBlockProps.mockClear(); | ||
} ); | ||
|
||
test( 'should add the has-alpha-channel-opacity class and no inline styles by default', () => { | ||
render( <SeparatorEdit { ...defaultProps } /> ); | ||
expect( useBlockProps ).toHaveBeenCalledWith( { | ||
className: 'has-alpha-channel-opacity', | ||
style: undefined, | ||
} ); | ||
} ); | ||
|
||
test( 'should add has-css-opacity class and no inline styles for deprecated block with no color specified', () => { | ||
const props = { | ||
...defaultProps, | ||
attributes: { ...defaultAttributes, opacity: 'css' }, | ||
}; | ||
render( <SeparatorEdit { ...props } /> ); | ||
expect( useBlockProps ).toHaveBeenCalledWith( { | ||
className: 'has-css-opacity', | ||
style: undefined, | ||
} ); | ||
} ); | ||
|
||
test( 'should add inline background style for block without dots style selected and custom color specified', () => { | ||
const props = { | ||
...defaultProps, | ||
attributes: { | ||
...defaultAttributes, | ||
style: { color: { background: '#ff0000' } }, | ||
}, | ||
}; | ||
render( <SeparatorEdit { ...props } /> ); | ||
expect( useBlockProps ).toHaveBeenCalledWith( { | ||
// For backwards compatibility the has-text-color class is also added even though it is only needed for | ||
// is-style-dots as this class was always added to v1 blocks, so may be expected by themes and plugins. | ||
className: 'has-text-color has-alpha-channel-opacity', | ||
style: { | ||
backgroundColor: '#ff0000', | ||
color: '#ff0000', | ||
}, | ||
} ); | ||
} ); | ||
|
||
test( 'should add inline color style for block with dots style selected and custom color specified', () => { | ||
const props = { | ||
...defaultProps, | ||
attributes: { | ||
...defaultAttributes, | ||
className: 'is-style-dots', | ||
style: { color: { background: '#ff0000' } }, | ||
}, | ||
}; | ||
render( <SeparatorEdit { ...props } /> ); | ||
expect( useBlockProps ).toHaveBeenCalledWith( { | ||
className: 'has-text-color has-alpha-channel-opacity', | ||
style: { | ||
backgroundColor: '#ff0000', | ||
color: '#ff0000', | ||
}, | ||
} ); | ||
} ); | ||
|
||
test( 'should add color class when color from palette specified', () => { | ||
const props = { | ||
...defaultProps, | ||
attributes: { | ||
...defaultAttributes, | ||
backgroundColor: 'banana', | ||
}, | ||
}; | ||
render( <SeparatorEdit { ...props } /> ); | ||
// Note that only the manual addition of the text color class can be checked as the | ||
// background color classes are added by useBlockProps which has to be mocked. | ||
expect( useBlockProps ).toHaveBeenCalledWith( { | ||
className: | ||
'has-text-color has-banana-color has-alpha-channel-opacity', | ||
style: undefined, | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.