-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix way prop insertBlockAfter is send. * Use HOC to wrap around gradient hook. * Improve gradient HOC to be more generic. * Revert "Fix way prop insertBlockAfter is send." This reverts commit e0d921d. * Make withGradient available to RN libs. * Move export of withGradient to the gradient folder.
- Loading branch information
1 parent
f6e4673
commit c711a16
Showing
5 changed files
with
122 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockEditContext } from '../block-edit'; | ||
|
||
export function __experimentalGetGradientClass( gradientSlug ) { | ||
if ( ! gradientSlug ) { | ||
return undefined; | ||
} | ||
return `has-${ gradientSlug }-gradient-background`; | ||
} | ||
|
||
/** | ||
* Retrieves the gradient value per slug. | ||
* | ||
* @param {Array} gradients Gradient Palette | ||
* @param {string} slug Gradient slug | ||
* | ||
* @return {string} Gradient value. | ||
*/ | ||
export function getGradientValueBySlug( gradients, slug ) { | ||
const gradient = find( gradients, [ 'slug', slug ] ); | ||
return gradient && gradient.gradient; | ||
} | ||
|
||
export function __experimentalGetGradientObjectByGradientValue( | ||
gradients, | ||
value | ||
) { | ||
const gradient = find( gradients, [ 'gradient', value ] ); | ||
return gradient; | ||
} | ||
|
||
/** | ||
* Retrieves the gradient slug per slug. | ||
* | ||
* @param {Array} gradients Gradient Palette | ||
* @param {string} value Gradient value | ||
* @return {string} Gradient slug. | ||
*/ | ||
export function getGradientSlugByValue( gradients, value ) { | ||
const gradient = __experimentalGetGradientObjectByGradientValue( | ||
gradients, | ||
value | ||
); | ||
return gradient && gradient.slug; | ||
} | ||
|
||
export function __experimentalUseGradient( { | ||
gradientAttribute = 'gradient', | ||
customGradientAttribute = 'customGradient', | ||
} = {} ) { | ||
const { clientId } = useBlockEditContext(); | ||
|
||
const { gradients, gradient, customGradient } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes, getSettings } = select( | ||
'core/block-editor' | ||
); | ||
const attributes = getBlockAttributes( clientId ); | ||
return { | ||
gradient: attributes[ gradientAttribute ], | ||
customGradient: attributes[ customGradientAttribute ], | ||
gradients: getSettings().gradients, | ||
}; | ||
}, | ||
[ clientId, gradientAttribute, customGradientAttribute ] | ||
); | ||
|
||
const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); | ||
const setGradient = useCallback( | ||
( newGradientValue ) => { | ||
const slug = getGradientSlugByValue( gradients, newGradientValue ); | ||
if ( slug ) { | ||
updateBlockAttributes( clientId, { | ||
[ gradientAttribute ]: slug, | ||
[ customGradientAttribute ]: undefined, | ||
} ); | ||
return; | ||
} | ||
updateBlockAttributes( clientId, { | ||
[ gradientAttribute ]: undefined, | ||
[ customGradientAttribute ]: newGradientValue, | ||
} ); | ||
}, | ||
[ gradients, clientId, updateBlockAttributes ] | ||
); | ||
|
||
const gradientClass = __experimentalGetGradientClass( gradient ); | ||
let gradientValue; | ||
if ( gradient ) { | ||
gradientValue = getGradientValueBySlug( gradients, gradient ); | ||
} else { | ||
gradientValue = customGradient; | ||
} | ||
return { gradientClass, gradientValue, setGradient }; | ||
} | ||
export * from './use-gradient'; |
2 changes: 2 additions & 0 deletions
2
packages/block-editor/src/components/gradients/index.native.js
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,2 @@ | ||
export * from './use-gradient'; | ||
export * from './with-gradient'; |
108 changes: 108 additions & 0 deletions
108
packages/block-editor/src/components/gradients/use-gradient.js
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,108 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockEditContext } from '../block-edit'; | ||
|
||
export function __experimentalGetGradientClass( gradientSlug ) { | ||
if ( ! gradientSlug ) { | ||
return undefined; | ||
} | ||
return `has-${ gradientSlug }-gradient-background`; | ||
} | ||
|
||
/** | ||
* Retrieves the gradient value per slug. | ||
* | ||
* @param {Array} gradients Gradient Palette | ||
* @param {string} slug Gradient slug | ||
* | ||
* @return {string} Gradient value. | ||
*/ | ||
export function getGradientValueBySlug( gradients, slug ) { | ||
const gradient = find( gradients, [ 'slug', slug ] ); | ||
return gradient && gradient.gradient; | ||
} | ||
|
||
export function __experimentalGetGradientObjectByGradientValue( | ||
gradients, | ||
value | ||
) { | ||
const gradient = find( gradients, [ 'gradient', value ] ); | ||
return gradient; | ||
} | ||
|
||
/** | ||
* Retrieves the gradient slug per slug. | ||
* | ||
* @param {Array} gradients Gradient Palette | ||
* @param {string} value Gradient value | ||
* @return {string} Gradient slug. | ||
*/ | ||
export function getGradientSlugByValue( gradients, value ) { | ||
const gradient = __experimentalGetGradientObjectByGradientValue( | ||
gradients, | ||
value | ||
); | ||
return gradient && gradient.slug; | ||
} | ||
|
||
export function __experimentalUseGradient( { | ||
gradientAttribute = 'gradient', | ||
customGradientAttribute = 'customGradient', | ||
} = {} ) { | ||
const { clientId } = useBlockEditContext(); | ||
|
||
const { gradients, gradient, customGradient } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes, getSettings } = select( | ||
'core/block-editor' | ||
); | ||
const attributes = getBlockAttributes( clientId ); | ||
return { | ||
gradient: attributes[ gradientAttribute ], | ||
customGradient: attributes[ customGradientAttribute ], | ||
gradients: getSettings().gradients, | ||
}; | ||
}, | ||
[ clientId, gradientAttribute, customGradientAttribute ] | ||
); | ||
|
||
const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); | ||
const setGradient = useCallback( | ||
( newGradientValue ) => { | ||
const slug = getGradientSlugByValue( gradients, newGradientValue ); | ||
if ( slug ) { | ||
updateBlockAttributes( clientId, { | ||
[ gradientAttribute ]: slug, | ||
[ customGradientAttribute ]: undefined, | ||
} ); | ||
return; | ||
} | ||
updateBlockAttributes( clientId, { | ||
[ gradientAttribute ]: undefined, | ||
[ customGradientAttribute ]: newGradientValue, | ||
} ); | ||
}, | ||
[ gradients, clientId, updateBlockAttributes ] | ||
); | ||
|
||
const gradientClass = __experimentalGetGradientClass( gradient ); | ||
let gradientValue; | ||
if ( gradient ) { | ||
gradientValue = getGradientValueBySlug( gradients, gradient ); | ||
} else { | ||
gradientValue = customGradient; | ||
} | ||
return { gradientClass, gradientValue, setGradient }; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/block-editor/src/components/gradients/with-gradient.js
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,9 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { __experimentalUseGradient } from './use-gradient'; | ||
|
||
export const withGradient = ( WrappedComponent ) => ( props ) => { | ||
const { gradientValue } = __experimentalUseGradient(); | ||
return <WrappedComponent { ...props } gradientValue={ gradientValue } />; | ||
}; |
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