PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 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 All 43 releases
tableberg / src / blocks / button / index.tsx

index.tsx in Tableberg – Simple Gutenberg Table Block 1.2.0, at src/blocks/button/index.tsx

124 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { CSSProperties } from "react";
2 import { RichText, useBlockProps } from "@wordpress/block-editor";
3 import { BlockEditProps, registerBlockType } from "@wordpress/blocks";
4 import { __ } from "@wordpress/i18n";
5 import { mergeAttrsWithDefaultsAndApplyBindings } from "@tableberg/shared/utils/merge-attrs-with-defaults-and-apply-bindings";
6 import buttonIcon from "@tableberg/shared/icons/button";
7
8 import {
9 ButtonElementAttributes,
10 buttonAttrDefaults,
11 getButtonVariantStyles,
12 } from "./element";
13 import metadata from "./block.json";
14 import { ButtonElementControls } from "./controls";
15 import { ElementBindings } from "../../dynamic-data/types";
16 import {
17 ElementAlignmentToolbar,
18 ElementStyleOptions,
19 NO_COORDS,
20 alignmentWrapperStyle,
21 } from "../element-edit-common";
22
23 function ButtonEdit({
24 attributes,
25 setAttributes,
26 isSelected,
27 }: BlockEditProps<ButtonElementAttributes>) {
28 const merged = mergeAttrsWithDefaultsAndApplyBindings(
29 attributes,
30 buttonAttrDefaults,
31 undefined,
32 {},
33 __("(No data)", "tableberg")
34 );
35 const { content, align, styles } = merged;
36
37 const blockProps = useBlockProps({
38 style: alignmentWrapperStyle(align),
39 });
40
41 const variantStyles = getButtonVariantStyles(styles);
42
43 const buttonStyle: CSSProperties = {
44 backgroundColor:
45 variantStyles.vars["--tableberg-button-background-color"] ||
46 undefined,
47 color:
48 variantStyles.vars["--tableberg-button-text-color"] || undefined,
49 fontSize: (styles as { fontSize?: string }).fontSize || undefined,
50 textAlign: styles.textAlign as CSSProperties["textAlign"],
51 width: styles.width === "auto" ? undefined : styles.width,
52 paddingTop: styles.padding?.top,
53 paddingRight: styles.padding?.right,
54 paddingBottom: styles.padding?.bottom,
55 paddingLeft: styles.padding?.left,
56 borderTopLeftRadius: styles.borderRadius?.topLeft,
57 borderTopRightRadius: styles.borderRadius?.topRight,
58 borderBottomRightRadius: styles.borderRadius?.bottomRight,
59 borderBottomLeftRadius: styles.borderRadius?.bottomLeft,
60 border: variantStyles.border ?? "none",
61 cursor: "text",
62 };
63
64 return (
65 <div {...blockProps}>
66 {isSelected && (
67 <ElementAlignmentToolbar
68 align={align}
69 onChange={newAlign => setAttributes({ align: newAlign })}
70 />
71 )}
72 {isSelected && (
73 <ElementStyleOptions
74 elementName="button"
75 attributes={merged as unknown as Record<string, unknown>}
76 setAttributes={
77 setAttributes as (
78 attrs: Record<string, unknown>
79 ) => void
80 }
81 />
82 )}
83 {isSelected && (
84 <ButtonElementControls
85 attributes={merged}
86 bindings={
87 (attributes as { bindings?: ElementBindings }).bindings
88 }
89 cellCoords={NO_COORDS}
90 elementIndex={0}
91 updateStyles={newStyles =>
92 setAttributes({
93 styles: {
94 ...(attributes.styles ?? merged.styles),
95 ...newStyles,
96 },
97 })
98 }
99 updateAttrs={attrs => setAttributes(attrs)}
100 />
101 )}
102 <RichText
103 tagName="div"
104 className="tableberg-button-element"
105 placeholder={__("Button text", "tableberg")}
106 value={content}
107 onChange={newContent =>
108 setAttributes({ content: newContent })
109 }
110 style={buttonStyle}
111 />
112 </div>
113 );
114 }
115
116 export function registerButtonBlock() {
117 registerBlockType(metadata.name, {
118 ...(metadata as any),
119 icon: buttonIcon,
120 edit: ButtonEdit,
121 save: () => null,
122 });
123 }
124