PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / shared / utils / styling-helpers.ts

styling-helpers.ts in Tableberg – Simple Gutenberg Table Block 1.1.5, at shared/utils/styling-helpers.ts

146 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 // @ts-ignore
3 isValueSpacingPreset,
4 } from "@wordpress/block-editor";
5 import { kebabCase } from "lodash";
6 import { HTMLAttributes } from "react";
7
8 export type StyleAttr = NonNullable<HTMLAttributes<HTMLDivElement>["style"]>;
9 export interface Spacing {
10 top?: string;
11 right?: string;
12 bottom?: string;
13 left?: string;
14 }
15
16 /**
17 * Converts a spacing preset into a custom value.
18 *
19 * @param {string} value Value to convert.
20 *
21 * @return {string | undefined} CSS var string for given spacing preset value.
22 */
23 export function getSpacingPresetCssVar(value: string) {
24 if (!value) {
25 return;
26 }
27
28 const slug = value.match(/var:preset\|spacing\|(.+)/);
29
30 if (!slug) {
31 return value;
32 }
33
34 return `var(--wp--preset--spacing--${slug[1]})`;
35 }
36
37 export function getSpacingCss(object: object): Spacing {
38 const css: Spacing = {};
39 //@ts-ignore
40 for (const [key, value] of Object.entries(object)) {
41 if (isValueSpacingPreset(value)) {
42 //@ts-ignore
43 css[key] = getSpacingPresetCssVar(value);
44 } else {
45 //@ts-ignore
46 css[key] = value;
47 }
48 }
49 return css;
50 }
51
52 export function getSpacingStyle(object: any, prefix: string): StyleAttr {
53 if (!object) {
54 return {};
55 }
56
57 const css: StyleAttr = {};
58 for (const side in object) {
59 const val = object[side];
60 // @ts-ignore
61 css[`${prefix}-${side}`] = isValueSpacingPreset(val)
62 ? getSpacingPresetCssVar(val)
63 : val;
64 }
65
66 return css;
67 }
68
69 export function getSpacingCssSingle(value: string) {
70 if (isValueSpacingPreset(value)) {
71 return getSpacingPresetCssVar(value);
72 } else {
73 return value;
74 }
75 }
76
77 export const getBorderCSS = (object: any): StyleAttr => {
78 if (!object) {
79 return {};
80 }
81 if (typeof object === "string") {
82 return {
83 border: object,
84 };
85 }
86 if (object.color || object.width) {
87 return {
88 borderWidth: object.width,
89 borderColor: object.color,
90 };
91 }
92
93 const css: StyleAttr = {};
94
95 ["top", "right", "bottom", "left"].forEach(side => {
96 const sideVal = object[side];
97 if (sideVal) {
98 // @ts-ignore
99 css[`border-${side}-width`] = sideVal.width;
100 // @ts-ignore
101 css[`border-${side}-color`] = sideVal.color;
102 }
103 });
104
105 return css;
106 };
107
108 export const getBorderRadiusCSS = (object: any): StyleAttr => {
109 if (!object) {
110 return {};
111 }
112 if (typeof object === "string") {
113 return {
114 borderRadius: object,
115 };
116 }
117 const css: StyleAttr = {};
118 for (const corner in object) {
119 const uCorner = corner.charAt(0).toUpperCase() + corner.slice(1);
120 // @ts-ignore
121 css[`border${uCorner}Radius`] = object[corner];
122 }
123 return css;
124 };
125
126 export const getBorderRadiusVar = (object: any, prefix: string): StyleAttr => {
127 if (!object) {
128 return {};
129 }
130 if (typeof object === "string") {
131 return {
132 [prefix + "-top-left"]: object,
133 [prefix + "-top-right"]: object,
134 [prefix + "-bottom-left"]: object,
135 [prefix + "-bottom-right"]: object,
136 } as any;
137 }
138 const css: StyleAttr = {};
139 for (const corner in object) {
140 const kCorner = kebabCase(corner);
141 // @ts-ignore
142 css[prefix + `-${kCorner}`] = object[corner];
143 }
144 return css;
145 };
146