Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a table pattern #168

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/pattern-library/components/Library.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import classnames from 'classnames';
import { toChildArray } from 'preact';
import { useState } from 'preact/hooks';

Expand Down Expand Up @@ -68,12 +69,25 @@ function Pattern({ children, title }) {
);
}

/**
* @typedef LibraryExampleProps
* @prop {import("preact").ComponentChildren} [children]
* @prop {string} [title]
* @prop {'split'|'wide'} [variant='split'] - Layout variant. Applies
* appropriate className.
* - Split (default) lays out in a row. Non-demo example content is rendered
* left, with demos right. Demos in this variant stack vertically.
* - Wide lays out in a full-width column. Non-example is rendered first,
* then a row to contain demos. Demos in this variant render next to each
* other in a single row.
*/

/**
* Render example content and optional Demo(s) for a pattern.
*
* @param {LibraryBaseProps} props
* @param {LibraryExampleProps} props
*/
function Example({ children, title }) {
function Example({ children, title, variant = 'split' }) {
const kids = toChildArray(children);

// Extract Demo components out of any children
Expand All @@ -84,30 +98,35 @@ function Example({ children, title }) {
const notDemos = kids.filter(kid => !demos.includes(kid));

return (
<div className="LibraryExample">
<div className={classnames('LibraryExample', `LibraryExample--${variant}`)}>
<div className="LibraryExample__content">
{title && <h3 className="LibraryExample__heading">{title}</h3>}
{notDemos}
</div>
<div className="LibraryExample__content">{demos}</div>
<div className="LibraryExample__demos">{demos}</div>
</div>
);
}

/**
* Render a "Demo", with optional source. This will render the children as
* provided in a tabbed container. If `withSource` is `true`, the JSX source
* of the children will be provided in a separate "Source" tab from the
* rendered Demo content.
*
* @typedef DemoProps
* @prop {import("preact").ComponentChildren} [children]
* @prop {boolean} [withSource=false] - Should the demo also render the source?
* When true, a "Source" tab will be rendered, which will display the JSX
* source of the Demo's children
* @prop {object} [style] - Inline styles to apply to the demo container
* @prop {string} [title]
*/

/**
* Render a "Demo", with optional source. This will render the children as
* provided in a tabbed container. If `withSource` is `true`, the JSX source
* of the children will be provided in a separate "Source" tab from the
* rendered Demo content.
*
* @param {DemoProps} props
*/
function Demo({ children, withSource = false, style = {} }) {
function Demo({ children, withSource = false, style = {}, title }) {
const [visibleTab, setVisibleTab] = useState('demo');
const source = toChildArray(children).map((child, idx) => {
return (
Expand All @@ -120,6 +139,7 @@ function Demo({ children, withSource = false, style = {} }) {
});
return (
<div className="LibraryDemo">
{title && <h4 className="LibraryDemo__header">{title}</h4>}
<div className="LibraryDemo__tabs">
<LabeledButton
onClick={() => setVisibleTab('demo')}
Expand Down
80 changes: 65 additions & 15 deletions src/pattern-library/components/patterns/ContainerPatterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ import Library from '../Library';

import { IconButton, LabeledButton } from '../../../';

function ListElements() {
return (
<>
<li>Alpha</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
<li>Juliett</li>
<li>Kilo</li>
<li>Lima</li>
<li>Mike</li>
<li>November</li>
<li>Oscar</li>
<li>Papa</li>
<li>Quebec</li>
<li>Romeo</li>
<li>Sierra</li>
<li>Tango</li>
<li>Uniform</li>
<li>Victor</li>
<li>Whiskey</li>
<li>XRay</li>
<li>Yankee</li>
<li>Zulu</li>
</>
);
}

export default function ContainerPatterns() {
const [showModalExample, setShowModalExample] = useState(false);
return (
Expand Down Expand Up @@ -231,21 +264,38 @@ export default function ContainerPatterns() {
<code>scrollbox</code>.
</p>
<Library.Demo withSource>
<div className="hyp-scrollbox" style="height: 150px; width:250px">
<ul className="hyp-u-padding hyp-u-vertical-spacing">
<li>Alpha</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
<li>Golf</li>
<li>Hotel</li>
<li>India</li>
<li>Juliet</li>
<li>Kilo</li>
<li>Lima</li>
</ul>
<div style="height:250px;width:250px">
<div className="hyp-scrollbox">
<ul className="hyp-u-padding hyp-u-vertical-spacing">
<ListElements />
</ul>
</div>
</div>
</Library.Demo>
</Library.Example>

<Library.Example title="Scrollbox with header offset">
<p>
The <code>scrollbox--with-header</code> pattern offsets the top
scroll-hinting shadow to accommodate one header-like element with a
touch-target height (currently 44px).
</p>

<Library.Demo withSource>
<div style="height:250px;width:250px">
<div className="hyp-scrollbox--with-header">
<div
className="hyp-u-layout-row--center hyp-u-border--bottom hyp-u-bg-color--grey-1"
style="position:sticky;top:0;min-height:44px;"
Comment on lines +288 to +289
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if having a .hyp-u-layout-table-header would help.

It could also include min-height: $touch-target-size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Tell you what. I was thinking of implementing a simple Scrollbox container component in my next body of work. I will look into introducing a pattern that corresponds to "a header of the correct size to put in a scrollbox" along the lines of what you're suggesting here. 👍🏻

>
<div>
<strong>NATO Phonetic Alphabet</strong>
</div>
</div>
<ul className="hyp-u-padding hyp-u-vertical-spacing">
<ListElements />
</ul>
</div>
</div>
</Library.Demo>
</Library.Example>
Expand Down
125 changes: 125 additions & 0 deletions src/pattern-library/components/patterns/TablePatterns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import Library from '../Library';

// "Fixture" for example table contents
function ExampleTBody() {
return (
<tbody>
<tr>
<td>Alphanumeric Balloons</td>
<td>Champagne Delusions</td>
</tr>
<tr>
<td>Elephantine Fry-ups</td>
<td>Gargantuan Hiccups</td>
</tr>
<tr className="is-selected">
<td>Illicit Jugglers</td>
<td>Katydid Lozenges Meringue</td>
</tr>
<tr>
<td>Alphanumeric Balloons</td>
<td>Champagne Delusions</td>
</tr>
<tr>
<td>Elephantine Fry-ups</td>
<td>Gargantuan Hiccups</td>
</tr>
<tr>
<td>Illicit Jugglers</td>
<td>Katydid Lozenges Moebius</td>
</tr>
<tr>
<td>Elephantine Fry-ups</td>
<td>Gargantuan Hiccups</td>
</tr>
<tr>
<td>Illicit Jugglers</td>
<td>Katydid Lozenges Meringue</td>
</tr>
<tr>
<td>Alphanumeric Balloons</td>
<td>Champagne Delusions</td>
</tr>
</tbody>
);
}

export default function TablePatterns() {
return (
<Library.Page title="Tables">
<p>
These <code>table</code> patterns support a basic table layout that
adapts to available space. They are intended for simpler tabular
display: maximum 2 or possibly 3 columns. Remember that{' '}
<code>table</code> content needs to be usable in tight and narrow
spaces.
</p>
<Library.Pattern title="Table">
<Library.Example title="Basic table" variant="wide">
<p>
By default, a <code>table</code> will fill available horizontal
space, and will use whatever height is needed to render its rows.{' '}
<code>tr.is-selected</code> styles a row as selected.
</p>

<Library.Demo withSource>
<table className="hyp-table">
<thead>
<tr>
<th scope="col">Column A</th>
<th scope="col">Column B</th>
</tr>
</thead>
<ExampleTBody />
</table>
</Library.Demo>
</Library.Example>

<Library.Example title="Adjusting column widths">
<p>
Table column widths may be adjusted by styling <code>thead th</code>{' '}
elements. In this example, the column widths are set to 30% and 70%.
</p>
<Library.Demo withSource>
<table className="hyp-table">
<thead>
<tr>
<th scope="col" style="width:30%">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never seen scope="col". (
TIL, according to MDN: If the scope attribute is not specified, or its value is not row, col, or rowgroup, or colgroup, then browsers automatically select the set of cells to which the header cell applies.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I also just TIL that particular detail. From the styling side, the table-layout: fixed rule in the SASS makes it so that the table and column width is derived from the first row in the table, making it possible to style the th widths and have that reliably apply to the whole table.

Column A
</th>
<th scope="col" style="width:70%">
Column B
</th>
</tr>
</thead>
<ExampleTBody />
</table>
</Library.Demo>
</Library.Example>

<Library.Example title="Constraining with a scrollbox">
<p>
In this example, the <code>table</code> is constrained within a{' '}
<code>scrollbox</code> with a <code>max-height</code>.
</p>
<Library.Demo withSource>
<div
style="max-height:250px"
className="hyp-scrollbox--with-header"
>
<table className="hyp-table">
<thead>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thead has a min-height of touch $touch-target-size, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, a height rule that behaves as a min-height, as min-height doesn't apply for elements with display: table-cell. But, yes, in concept.

<tr>
<th scope="col">Column A</th>
<th scope="col">Column B</th>
</tr>
</thead>
<ExampleTBody />
</table>
</div>
</Library.Demo>
</Library.Example>
</Library.Pattern>
</Library.Page>
);
}
7 changes: 7 additions & 0 deletions src/pattern-library/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FormPatterns from './components/patterns/FormPatterns';
import ContainerPatterns from './components/patterns/ContainerPatterns';
import PanelPatterns from './components/patterns/PanelPatterns';
import SpinnerPatterns from './components/patterns/SpinnerPatterns';
import TablePatterns from './components/patterns/TablePatterns';
import ThumbnailPatterns from './components/patterns/ThumbnailPatterns';

import ButtonComponents from './components/patterns/ButtonComponents';
Expand Down Expand Up @@ -73,6 +74,12 @@ const routes = [
component: SpinnerPatterns,
group: 'patterns',
},
{
route: '/patterns-tables',
title: 'Tables',
component: TablePatterns,
group: 'patterns',
},
{
route: '/patterns-thumbnails',
title: 'Thumbnails',
Expand Down
38 changes: 31 additions & 7 deletions styles/mixins/patterns/_containers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

$-border-radius: var.$border-radius;
$-color-background: var.$color-background;
$-header-height: var.$touch-target-size;

/**
* Patterns that are composites of multiple atomic utilities, but
Expand Down Expand Up @@ -182,32 +183,55 @@ $-color-background: var.$color-background;
* - Only the bottom scroll-hint shadow appears if content overflows
* - The bottom scroll-hint shadow is always present, even if content is
* fully scrolled
*
* @param {CSSLength} [$shadow-top-position=0] - Top scroll-indicating shadow
* (y) position. Default 0: at top edge of scrollbox. Use case: move
* shadow down to accommodate a sticky header of a known height, such that the
* shadow appears below the header(s).
* @param {CSSLength} [$shadow-bottom-position=100%] - Bottom scroll-indicating
* shadow position relative to scrollbox. Default 100%: flush to bottom.
*/
@mixin scrollbox {
@mixin scrollbox($shadow-top-position: 0, $shadow-bottom-position: 100%) {
overflow: auto;
@include atoms.border;
position: relative;
height: 100%;
width: 100%;

background:
// Shadow covers
linear-gradient($-color-background 30%, rgba(255, 255, 255, 0)),
linear-gradient(rgba(255, 255, 255, 0), $-color-background 70%) 0 100%,
// Shadows
// Top Shadow cover
linear-gradient($-color-background 30%, rgba(255, 255, 255, 0)) 0
$shadow-top-position,
// Bottom shadow cover
linear-gradient(rgba(255, 255, 255, 0), $-color-background 70%) 0
$shadow-bottom-position,
// Top shadow
linear-gradient(
to bottom,
rgba(0, 0, 0, 0.1),
rgba(0, 0, 0, 0.05) 5px,
rgba(255, 255, 255, 0) 70%
),
)
0 $shadow-top-position,
// Bottom shadow
linear-gradient(
to top,
rgba(0, 0, 0, 0.1),
rgba(0, 0, 0, 0.05) 5px,
rgba(255, 255, 255, 0) 70%
)
0 100%;
0 $shadow-bottom-position;
background-repeat: no-repeat;
background-color: $-color-background;
background-size: 100% 40px, 100% 40px, 100% 14px, 100% 14px;

background-attachment: local, local, scroll, scroll;
}

/**
* A scrollbox with the top shadow repositioned down by touch-target-size
* to accommodate, e.g., a sticky header at the top of the scrollable content.
*/
@mixin scrollbox--with-header {
@include scrollbox($shadow-top-position: $-header-height);
}
Loading