Skip to content

Commit e4e2a70

Browse files
authored
Merge pull request #135 from Automattic/configure-height
Set height of block
2 parents b61cd96 + 0a4997a commit e4e2a70

File tree

7 files changed

+82
-15
lines changed

7 files changed

+82
-15
lines changed

block/block.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
"roomId": {
1616
"type": "string",
1717
"default": ""
18+
},
19+
"height": {
20+
"type": "object",
21+
"default": {
22+
"value": 600,
23+
"unit": "px"
24+
}
1825
}
1926
},
2027
"supports": {

block/edit.tsx

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
import { useBlockProps } from "@wordpress/block-editor";
2+
import { ResizableBox } from "@wordpress/components";
23
import { WPElement } from '@wordpress/element';
34
import './editor.scss';
45
import IFrame from "./iframe";
5-
import Inspector from "./inspector";
6+
import InspectorControls from "./inspector";
7+
8+
type Height = {
9+
value: number
10+
unit: string
11+
}
612

713
export default function Edit({ attributes, setAttributes }): WPElement {
14+
const height: Height = attributes.height;
15+
16+
const heightWithUnit =
17+
height.value && height.unit
18+
? `${height.value}${height.unit}`
19+
: '';
20+
821
return (
9-
<div {...useBlockProps()}>
10-
<Inspector attributes={attributes} setAttributes={setAttributes}/>
11-
<IFrame attributes={attributes} focusable={true}/>
12-
</div>
22+
<>
23+
<InspectorControls attributes={attributes} setAttributes={setAttributes}/>
24+
<div {...useBlockProps()}>
25+
<ResizableBox
26+
size={{
27+
width: "100%",
28+
height: heightWithUnit,
29+
}}
30+
enable={{
31+
top: false,
32+
right: false,
33+
bottom: true,
34+
left: false,
35+
topRight: false,
36+
bottomRight: false,
37+
bottomLeft: false,
38+
topLeft: false,
39+
}}
40+
onResizeStop={(_event, _direction, elt) => {
41+
setAttributes({ height: { value: elt.clientHeight, unit: "px" } });
42+
}}
43+
>
44+
<IFrame props={{ height: heightWithUnit }} attributes={attributes} focusable={true}/>
45+
</ResizableBox>
46+
</div>
47+
</>
1348
);
1449
}

block/iframe.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { WPElement } from "@wordpress/element";
33
import { IframeUrl } from "../frontend/parent/iframe";
44
import { containerClass, iframeClass } from "../frontend/parent/util";
55

6-
export default function IFrame({ attributes, focusable = false }): WPElement {
6+
export default function IFrame({ props, attributes, focusable = false }): WPElement {
77
const ref = focusable ? useFocusableIframe() : undefined;
88
const url = iframeUrl(attributes);
99

1010
return (
11-
<div className={containerClass()}>
11+
<div className={containerClass()} style={{ height: props.height || undefined }}>
1212
<iframe className={iframeClass()}
1313
// @ts-ignore
1414
ref={ref}

block/inspector.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { InspectorControls } from "@wordpress/block-editor";
1+
import { InspectorControls as BaseInspectorControls } from "@wordpress/block-editor";
22
import { PanelBody, PanelRow, TextControl } from "@wordpress/components";
33
import { WPElement } from "@wordpress/element";
44
import { __ } from "@wordpress/i18n";
55

6-
export default function Inspector({ attributes, setAttributes }): WPElement {
6+
export default function InspectorControls({ attributes, setAttributes }): WPElement {
77
return (
8-
<InspectorControls>
8+
<BaseInspectorControls>
99
<PanelBody
1010
title={__("Homeserver", "chatrix")}
1111
initialOpen={true}
@@ -31,7 +31,22 @@ export default function Inspector({ attributes, setAttributes }): WPElement {
3131
/>
3232
</PanelRow>
3333
</PanelBody>
34-
</InspectorControls>
34+
<PanelBody
35+
title={__("Layout", "chatrix")}
36+
initialOpen={false}
37+
>
38+
<PanelRow>
39+
<TextControl
40+
label={__("height", "chatrix")}
41+
value={attributes.height.value}
42+
onChange={(value) => {
43+
setAttributes({ height: { value: value, unit: "px" } });
44+
}}
45+
help={"In pixels"}
46+
/>
47+
</PanelRow>
48+
</PanelBody>
49+
</BaseInspectorControls>
3550
);
3651
}
3752

block/style.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
overflow: hidden;
1010

1111
.automattic-chatrix-container {
12-
height: 600px;
13-
1412
iframe {
1513
width: 100%;
1614
height: 100%;

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Block/block.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ function () use ( $block_json_path ) {
3232
function render( array $attributes ): string {
3333
ob_start();
3434
$container_id = 'wp-block-automattic-chatrix-container';
35+
36+
$style = array(
37+
'height' => "{$attributes['height']['value']}{$attributes['height']['unit']}",
38+
);
39+
$style_attr = '';
40+
array_walk(
41+
$style,
42+
function ( $value, $key ) use ( &$style_attr ) {
43+
$style_attr .= "$key: $value;";
44+
}
45+
);
46+
3547
?>
3648
<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>>
37-
<div id="<?php echo esc_attr( $container_id ); ?>">
49+
<div id="<?php echo esc_attr( $container_id ); ?>" style="<?php echo esc_attr( $style_attr ); ?>">
3850
<?php // Iframe will be rendered here. ?>
3951
</div>
4052
<script>

0 commit comments

Comments
 (0)