-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class mechanism for preset gradients. (#18008)
- Loading branch information
1 parent
711f9ff
commit f0dc07c
Showing
8 changed files
with
201 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* 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`; | ||
} | ||
|
||
function getGradientValueBySlug( gradients, slug ) { | ||
const gradient = find( gradients, [ 'slug', slug ] ); | ||
return gradient && gradient.gradient; | ||
} | ||
|
||
function getGradientSlugByValue( gradients, value ) { | ||
const gradient = find( gradients, [ 'gradient', 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 ] ); | ||
|
||
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 }; | ||
} |
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
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