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 2 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
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);
}
44 changes: 35 additions & 9 deletions styles/pattern-library.scss
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,49 @@ $-library-vertical-spacing: 7;
.LibraryExample {
// Narrower screen/default shows single column
@include layout.vertical-spacing($size: $-library-vertical-spacing);
}

@mixin library-row {
// Turn off any applied vertical spacing
@include layout.vertical-spacing($size: 0);
@include layout.row;
@include layout.horizontal-spacing($size: 5);
}

// Wider screen shows description and demo side-by-side
.LibraryExample--split {
// Wider screen shows description and demo side-by-side (where space allows)
@media screen and (min-width: 60em) {
@include layout.row;
// Turn off vertical-spacing, as content is side-by-side
@include layout.vertical-spacing($size: 0);
@include layout.horizontal-spacing($size: 6);
@include library-row;

.LibraryExample__content {
.LibraryExample__content,
.LibraryExample__demos {
width: 50%;
@include layout.vertical-spacing($size: $-library-vertical-spacing);
}
}
}

.LibraryExample--wide {
// Show example content full width, and then a row containing demos
// side-by-side (where space allows)
.LibraryExample__demos {
@include layout.vertical-spacing($size: $-library-vertical-spacing);

@media screen and (min-width: 60em) {
@include library-row;
}
}
}

.LibraryDemo {
// When laid out in a row, make demos share space evenly
flex: 1 1 0px;

&__header {
font-size: 1.1em;
margin: 0.5rem 0;
}

&__tabs {
@include layout.row;
@include layout.horizontal-spacing;
Expand All @@ -277,16 +304,15 @@ $-library-vertical-spacing: 7;
}

&__container {
@include containers.frame;
@include layout.padding($size: 5, $side: top);
@include atoms.border(top);
@include layout.row($align: center, $justify: center);
// Make the demo take up at least a minimal amount of vertical space
min-height: 8rem;
background-color: var.$color-grey-1;
}

&__source,
&__demo {
@include containers.frame;
width: 100%;
}

Expand Down
4 changes: 4 additions & 0 deletions styles/patterns/_containers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@
.hyp-scrollbox {
@include containers.scrollbox;
}

.hyp-scrollbox--with-header {
@include containers.scrollbox--with-header;
}