Skip to content

Commit

Permalink
Polish the Table block
Browse files Browse the repository at this point in the history
This branch is based off of `try/better-responsive-table`, but without my attempt at better responsiveness.

- New toggle to decide whether table has fixed widths, off by default, as discussed in #2620
- Surface and style the resizing tool
  • Loading branch information
Joen Asmussen committed Apr 20, 2018
1 parent cb1b92e commit 64bd056
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
23 changes: 21 additions & 2 deletions blocks/library/table/editor.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
.wp-block-table {
table {
border-collapse: collapse;
width: 100%;
}

td,
th {
padding: 0.5em;
border: 1px solid currentColor;
}

td[data-mce-selected="1"], th[data-mce-selected="1"] {
background-color: $light-gray-500;
td[data-mce-selected="1"],
th[data-mce-selected="1"] {
background-color: $light-gray-300;
}
}

// Style the resize handles
.ephox-snooker-resizer-rows {
cursor: row-resize;
}

.ephox-snooker-resizer-cols {
cursor: col-resize;
}

.ephox-snooker-resizer-bar-dragging {
background: $blue-medium-500;
}
53 changes: 46 additions & 7 deletions blocks/library/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { __ } from '@wordpress/i18n';
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import {
PanelBody,
ToggleControl,
} from '@wordpress/components';
import classnames from 'classnames';

/**
* Internal dependencies
Expand All @@ -16,6 +21,7 @@ import './style.scss';
import TableBlock from './table-block';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import InspectorControls from '../../inspector-controls';

export const name = 'core/table';

Expand All @@ -32,14 +38,18 @@ export const settings = {
selector: 'table',
default: [
<tbody key="1">
<tr><td><br /></td><td><br /></td></tr>
<tr><td><br /></td><td><br /></td></tr>
<tr><td><br /></td><td><br /></td><td><br /></td></tr>
<tr><td><br /></td><td><br /></td><td><br /></td></tr>
</tbody>,
],
},
align: {
type: 'string',
},
hasFixedLayout: {
type: 'boolean',
default: false,
},
},

transforms: {
Expand All @@ -59,8 +69,19 @@ export const settings = {
},

edit( { attributes, setAttributes, isSelected, className } ) {
const { content } = attributes;
const { content, hasFixedLayout } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const toggleFixedLayout = () => {
setAttributes( { hasFixedLayout: ! hasFixedLayout } );
};

const classes = classnames(
className,
{
'has-fixed-layout': hasFixedLayout,
},
);

return (
<Fragment>
<BlockControls>
Expand All @@ -69,22 +90,40 @@ export const settings = {
onChange={ updateAlignment }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Table Settings' ) } className="blocks-table-settings">
<ToggleControl
label={ __( 'Fixed width table cells' ) }
checked={ !! hasFixedLayout }
onChange={ toggleFixedLayout }
/>
</PanelBody>
</InspectorControls>
<TableBlock
onChange={ ( nextContent ) => {
setAttributes( { content: nextContent } );
} }
content={ content }
className={ className }
className={ classes }
isSelected={ isSelected }
/>
</Fragment>
);
},

save( { attributes } ) {
const { content, align } = attributes;
save( { attributes, className } ) {
const { content, align, hasFixedLayout } = attributes;

const classes = classnames(
className,
{
'has-fixed-layout': hasFixedLayout,
},
align ? `align${ align }` : null,
);

return (
<table className={ align ? `align${ align }` : null }>
<table className={ classes }>
{ content }
</table>
);
Expand Down
17 changes: 15 additions & 2 deletions blocks/library/table/style.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
.wp-block-table {
overflow-x: auto;
display: block;
border-collapse: collapse;
width: 100%;

table {
border-collapse: collapse;
tbody {
width: 100%;
display: table;
}

td,
th {
padding: 0.5em;
border: 1px solid currentColor;
}

// Fixed layout toggle
&.has-fixed-layout tbody {
table-layout: fixed;
}
}

0 comments on commit 64bd056

Please sign in to comment.