-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: Add gradient text format #62
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,57 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { RichTextToolbarButton } from '@wordpress/block-editor'; | ||
| import { useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import InlineUI from './inline-ui'; | ||
| import GradientTextIcon from './icon'; | ||
|
|
||
| /** | ||
| * Edit component for the Infotip format. | ||
| * | ||
| * @param {Object} props - The component properties. | ||
| * @param {Object} props.value - The current value of the rich text. | ||
| * @param {Function} props.onChange - Function to update the rich text value. | ||
| * @param {Function} props.onFocus - Function to handle focus events. | ||
| * @param {boolean} props.isActive - Indicates if the format is currently active. | ||
| * @param {Object} props.contentRef - Reference to the editable content element. | ||
| * @param {Object} props.activeAttributes - The currently active attributes. | ||
| * @return {JSX.Element} - The rendered gradient text button. | ||
| */ | ||
| export function Edit({ | ||
| value, | ||
| onChange, | ||
| onFocus, | ||
| isActive, | ||
| contentRef, | ||
| activeAttributes, | ||
| }) { | ||
| const [isSettingOpen, setIsSettingOpen] = useState(false); | ||
|
|
||
| return ( | ||
| <> | ||
| <RichTextToolbarButton | ||
| icon={<GradientTextIcon />} | ||
| title={__('Gradient Text', 'blablablocks-formats')} | ||
| onClick={() => setIsSettingOpen(true)} | ||
| isActive={isActive} | ||
| /> | ||
| {isSettingOpen && ( | ||
| <InlineUI | ||
| activeAttributes={activeAttributes} | ||
| onClose={() => setIsSettingOpen(false)} | ||
| contentRef={contentRef.current} | ||
| isActive={isActive} | ||
| value={value} | ||
| onChange={onChange} | ||
| onFocus={onFocus} | ||
| /> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
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,17 @@ | ||
| .block-editor-format-toolbar__blablablocks-gradient-text-popover .components-popover__content>div { | ||
| padding: 16px; | ||
| width: 260px; | ||
| } | ||
|
|
||
| .block-editor-format-toolbar__blablablocks-gradient-text-popover .components-custom-gradient-picker__gradient-bar .components-custom-gradient-picker__control-point-button { | ||
| padding: 0px; | ||
| } | ||
|
|
||
| .block-editor-format-toolbar__blablablocks-gradient-text-popover { | ||
|
|
||
| .reset-button { | ||
| margin-top: 1rem; | ||
| display: block; | ||
| margin-left: auto; | ||
| } | ||
| } |
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,24 @@ | ||
| /** | ||
| * GradientText component renders an SVG icon that represents a gradient text. | ||
| * | ||
| * @return {JSX.Element} SVG component for a gradient text icon. | ||
| */ | ||
| function GradientTextIcon() { | ||
| return ( | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| xmlSpace="preserve" | ||
| style={{ | ||
| enableBackground: "new 0 0 32 32", | ||
| }} | ||
| width={"24px"} | ||
| height={"24px"} | ||
| viewBox="0 -4 32 40" | ||
| > | ||
| <path d="M26 3H6C4.3 3 3 4.3 3 6v20c0 1.7 1.3 3 3 3h20c1.7 0 3-1.3 3-3V6c0-1.7-1.3-3-3-3zm0 24h-1v-2h-3v2h-3v-2h-3v2h-3v-2h-3v2H7v-2H5v-3h2v-3H5V6c0-.6.4-1 1-1h20c.6 0 1 .4 1 1v10h-2v3h2v3h-2v3h2v1c0 .6-.4 1-1 1z" /> | ||
| <path d="M7 22h3v3H7zM13 22h3v3h-3zM19 22h3v3h-3zM10 19h3v3h-3zM16 19h3v3h-3zM22 19h3v3h-3zM7 16h3v3H7zM13 16h3v3h-3zM19 16h3v3h-3z" /> | ||
| </svg> | ||
| ); | ||
| } | ||
|
|
||
| export default GradientTextIcon; |
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,25 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { registerFormatType } from '@wordpress/rich-text'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './editor.scss'; | ||
| import './style.scss'; | ||
| import { Edit } from './edit'; | ||
|
|
||
| /** | ||
| * Registers the Gradient Text format type. | ||
| */ | ||
| registerFormatType('blablablocks/gradient-text', { | ||
| title: __('Gradient Text', 'blablablocks-formats'), | ||
| tagName: 'gradient-text', | ||
| className: 'has-gradient-text', | ||
| edit: Edit, | ||
| attributes: { | ||
| style: 'style', | ||
| }, | ||
| }); |
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,94 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { removeFormat, useAnchor } from '@wordpress/rich-text'; | ||
| import { | ||
| __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients | ||
| } from '@wordpress/block-editor'; | ||
| import { Popover, GradientPicker } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { createFormatHelpers } from '../utils'; | ||
|
|
||
| /** | ||
| * InlineUI component for handling Gradient text formatting options. | ||
| * | ||
| * @param {Object} props - The component properties. | ||
| * @param {Object} props.activeAttributes - The currently active attributes. | ||
| * @param {Object} props.value - The current value of the rich text. | ||
| * @param {Function} props.onChange - Function to update the rich text value. | ||
| * @param {Function} props.onClose - Function to close the popover. | ||
| * @param {Object} props.contentRef - Reference to the editable content element. | ||
| * @param {boolean} props.isActive - Indicates if the format is currently active. | ||
| * @return {JSX.Element} - The rendered InlineUI component. | ||
| */ | ||
| function InlineUI({ | ||
| activeAttributes, | ||
| value, | ||
| onChange, | ||
| onClose, | ||
| contentRef, | ||
| isActive, | ||
| }) { | ||
| const name = 'blablablocks/gradient-text'; // Format type name | ||
| const PREFIX = 'background:'; | ||
|
|
||
| // safely extract the raw gradient for the picker | ||
| const stored = activeAttributes?.style ?? ''; | ||
| const pickerValue = stored.startsWith(PREFIX) ? stored.slice(PREFIX.length) : stored; | ||
|
|
||
| const { update } = createFormatHelpers({ | ||
| value, | ||
| onChange, | ||
| formatType: name, | ||
| activeAttributes, | ||
| }); | ||
|
|
||
| const anchor = useAnchor({ | ||
| editableContentElement: contentRef, | ||
| settings: { isActive }, | ||
| }); | ||
|
|
||
| const colorGradientSettings = useMultipleOriginColorsAndGradients(); | ||
|
|
||
| return ( | ||
| <Popover | ||
| anchor={anchor} | ||
| className="block-editor-format-toolbar__blablablocks-gradient-text-popover" | ||
| position="middle center" | ||
| onClose={onClose} | ||
| offset={30} | ||
| shift | ||
| __unstableSlotName="__unstable-block-tools-after" | ||
| > | ||
| <GradientPicker | ||
| value={pickerValue || undefined} | ||
| gradients={colorGradientSettings.gradients} | ||
| onChange={(gradient) => { | ||
| if (!gradient) { | ||
| // remove the style | ||
| return update({ style: '' }); | ||
| } | ||
| update({ style: `${PREFIX}${gradient}` }); | ||
| }} | ||
| /> | ||
| <Button | ||
| accessibleWhenDisabled | ||
| className="reset-button" | ||
| onClick={() => { | ||
| onChange(removeFormat(value, name)) | ||
| onClose(); | ||
| }} | ||
| variant="tertiary" | ||
| __next40pxDefaultSize | ||
| > | ||
| {__('Clear', 'blablablocks-formats')} | ||
| </Button> | ||
| </Popover> | ||
| ); | ||
| } | ||
|
|
||
| export default InlineUI; | ||
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,4 @@ | ||
| gradient-text { | ||
| -webkit-background-clip: text !important; | ||
| -webkit-text-fill-color: transparent; | ||
| } |
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 |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
| */ | ||
| import './marker'; | ||
| import './infotip'; | ||
| import './gradient-text'; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.