PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.2
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / formats / badge / index.js

index.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.2, at gutenberg/formats/badge/index.js

173 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { RichTextToolbarButton } from '@wordpress/block-editor';
2 import { TabPanel } from '@wordpress/components';
3 import { useEffect, useState } from '@wordpress/element';
4 import { __ } from '@wordpress/i18n';
5 import {
6 applyFormat,
7 getActiveFormat,
8 toggleFormat,
9 } from '@wordpress/rich-text';
10
11 import ColorPalette from '../../components/color-palette';
12 import getIcon from '../../utils/get-icon';
13 import BadgePopover from './badge-popover';
14
15 export const name = 'ghostkit/badge';
16
17 function parseCSS(css = '') {
18 return css.split(';').reduce((accumulator, rule) => {
19 if (rule) {
20 let [property, value] = rule.split(':');
21
22 if (property) {
23 property = property.trim();
24 }
25 if (value) {
26 value = value.trim();
27 }
28
29 if (property === 'color') {
30 accumulator.color = value;
31 }
32
33 if (property === 'background') {
34 accumulator.background = value;
35 }
36 }
37
38 return accumulator;
39 }, {});
40 }
41
42 function BadgeFormat(props) {
43 const { isActive, value, contentRef, onChange } = props;
44
45 const [openedPopover, setOpenedPopover] = useState(false);
46
47 // Close popover.
48 useEffect(() => {
49 if (!isActive && openedPopover) {
50 setOpenedPopover(false);
51 }
52 }, [isActive, openedPopover]);
53
54 function getCurrentColor() {
55 const { attributes } = getActiveFormat(value, name);
56
57 if (attributes && attributes.style) {
58 return parseCSS(attributes.style);
59 }
60
61 return {};
62 }
63
64 function toggleBadge(newBackground, newColor) {
65 const attributes = {};
66
67 if (newBackground) {
68 attributes.style = `background: ${newBackground};`;
69 }
70 if (newColor) {
71 if (attributes.style) {
72 attributes.style += ' ';
73 } else {
74 attributes.style = '';
75 }
76
77 attributes.style += `color: ${newColor};`;
78 }
79
80 const runFormat = newBackground ? applyFormat : toggleFormat;
81
82 onChange(
83 runFormat(value, {
84 type: name,
85 attributes,
86 })
87 );
88 }
89
90 let currentColor = {};
91
92 if (isActive) {
93 currentColor = getCurrentColor();
94 }
95
96 return (
97 <>
98 <RichTextToolbarButton
99 icon={getIcon('icon-badge')}
100 title={__('Badge', 'ghostkit')}
101 onClick={() => {
102 if (!isActive) {
103 toggleBadge();
104 }
105
106 setOpenedPopover(!openedPopover);
107 }}
108 isActive={isActive}
109 />
110 {isActive && openedPopover ? (
111 <BadgePopover value={value} name={name} contentRef={contentRef}>
112 <TabPanel
113 tabs={[
114 {
115 name: 'background',
116 title: 'Background',
117 },
118 {
119 name: 'text',
120 title: 'Text',
121 },
122 ]}
123 initialTabName="background"
124 >
125 {(tab) => {
126 if ('text' === tab.name) {
127 return (
128 <ColorPalette
129 label={__('Text', 'ghostkit')}
130 value={currentColor?.color || ''}
131 onChange={(val) => {
132 toggleBadge(
133 currentColor?.background || '',
134 val
135 );
136 }}
137 alpha
138 />
139 );
140 }
141
142 return (
143 <ColorPalette
144 label={__('Background', 'ghostkit')}
145 value={currentColor?.background || ''}
146 onChange={(val) => {
147 toggleBadge(
148 val,
149 currentColor?.color || ''
150 );
151 }}
152 alpha
153 gradient
154 />
155 );
156 }}
157 </TabPanel>
158 </BadgePopover>
159 ) : null}
160 </>
161 );
162 }
163
164 export const settings = {
165 title: __('Badge', 'ghostkit'),
166 tagName: 'span',
167 className: 'ghostkit-badge',
168 attributes: {
169 style: 'style',
170 },
171 edit: BadgeFormat,
172 };
173