PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / trunk
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts vtrunk
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / src / features / custom-css / index.js

index.js in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts trunk, at src/features/custom-css/index.js

74 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * External dependencies
3 */
4 import clsx from "clsx";
5
6 /**
7 * WordPress dependencies
8 */
9 import { __ } from "@wordpress/i18n";
10 import { addFilter } from "@wordpress/hooks";
11 import { createHigherOrderComponent } from "@wordpress/compose";
12
13 /**
14 * Internal dependencies
15 */
16 import { boldblocksHasSupport, refineCustomCSS } from "../../utils";
17
18 /**
19 * Define feature name
20 */
21 const featureName = "customCSS";
22
23 /**
24 * If the current block support custom CSS
25 *
26 * @param {String} name
27 * @returns {Boolean}
28 */
29 export const hasSupportCSS = (name) =>
30 boldblocksHasSupport(name, featureName) ||
31 ["core/template-part"].includes(name);
32
33 /**
34 * Override the default block list element to add custom styles.
35 *
36 * @param {Function} BlockListBlock Original component
37 * @return {Function} Wrapped component
38 */
39 export const withCustomStyles = createHigherOrderComponent(
40 (BlockListBlock) => (props) => {
41 if (!hasSupportCSS(props.name)) {
42 return <BlockListBlock {...props} />;
43 }
44
45 const {
46 attributes: { boldblocks: { customCSS: { value = "" } = {} } = {} },
47 clientId,
48 } = props;
49
50 const selector = `b-${clientId}`;
51 let style;
52 if (value) {
53 style = refineCustomCSS(value, { selector: `.${selector}` });
54 }
55
56 return (
57 <>
58 <BlockListBlock
59 {...props}
60 className={clsx(props?.className, {
61 [selector]: style,
62 })}
63 />
64 {style && <style>{style}</style>}
65 </>
66 );
67 },
68 );
69 addFilter(
70 "editor.BlockListBlock",
71 `boldblocks/${featureName}/withCustomStyles`,
72 withCustomStyles,
73 );
74