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

Add Stack component #2152

Merged
merged 26 commits into from
Jul 25, 2022
Merged

Add Stack component #2152

merged 26 commits into from
Jul 25, 2022

Conversation

vdepizzol
Copy link
Contributor

@vdepizzol vdepizzol commented Jul 12, 2022

What are you trying to accomplish?

This PR adds the Stack component to Primer CSS.

A Stack component lays elements horizontally or vertically on the page.

Stack is a simple abstraction of CSS’ Flexbox. Use it to structure elements that are visually grouped and follow the same direction.

Overview

This is the first Layout helper component1 to be designed and implemented following the new Responsive API design guidelines2.

Layout helper components have been proposed as a way to reduce our dependency on utilities, make it easier to implement responsive design, and support the new Primer Primitives design tokens.

Layout helper components are designed in a platform-agnostic way. They will be available to both Primer ViewComponents and Primer React with the same behavior and API.

Features

Stack makes it easy to group elements that follow a same direction (vertically or horizontally), with spacing gap, optional dividers, and optional wrapping.

Responsive by design

All properties from this component are responsive, meaning different values can be passed depending on the viewport range.

For example, a Stack direction can be set to inline (horizontal) on narrow, and block (vertical) on desktop.

In CSS, we use -whenNarrow, -whenRegular, and -whenWide suffixes to target modifier classes to a specific range. This is how the team is planning to support any responsive properties going forward.

More information about viewport ranges can be found here (only available for Hubbers, documentation to be made public in a future PR).

Alignment and distribution of stacking elements

Stack also allows aligning elements without relying on difficult Flexbox property names (align-items, justify-content, and align-content). For that, it uses simpler concepts such as align and spread (described in the Properties section below).

<!-- Vertical stacking, with elements aligned to the right -->
<Stack direction="block" align="end">
  <div class="Box">Element 1</div>
  <div class="Box">Element 2</div>
  <div class="Box">Element 3</div>
</Stack>
<!-- Horizontal buttons, positioned to the right -->
<Stack direction="inline" spread="end">
  <button>Cancel</button>
  <button>Save</button>
</Stack>
Note on using inline Stack for buttons

In the future we may have specialized stacking components for controls using the UI control stack tokens.

In the meantime, we can use these tokens with a custom gap:

<Stack direction="inline" spread="end" gap="var(--primer-controlStack-medium-gap-auto)">
  <button>Cancel</button>
  <button>Save</button>
</Stack>

Properties

Stack has the following properties:

direction

Sets how elements inside Stack are placed, either horizontally (inline) or vertically (block). This property follows the writing mode.

gap

Sets the spacing gap between items. All sizes use design tokens from the new Primer Primitives' UI Stack pattern.

Values:

  • none: 0
  • condensed: var(--primer-stack-gap-condensed, 8px),
  • normal: var(--primer-stack-gap-normal, 16px) (default)
  • spacious: var(--primer-stack-gap-spacious, 24px) (on regular viewports, otherwise it appears as normal on narrow viewports)

Custom values are not supported for now.

align

Sets the alignment between items in the cross-axis of the specified direction. For example:

  • If direction is set to block (stacks vertically), it controls the horizontal alignment (left, center, right).
  • If direction is set to inline (stacks horizontally), it controls the vertical alignment (top, center, bottom).

    This property behavior is equivalent to the align-items Flexbox property.

Values:

  • stretch (default)
  • start
  • center
  • end
  • baseline

alignWrap

Sets how stack lines are distributed, if they wrap into multiple lines. This has equivalent behavior to the align-content Flexbox property.

(Most instances of Stack shouldn't require this property)

Values:

  • start (default)
  • center
  • end
  • distribute (equivalent behavior to Flexbox's space-between)
  • distributeEvenly (equivalent behavior to Flexbox's space-evenly)

spread

Sets how items will be distributed in the stacking direction.

Values:

  • start (default)
  • center
  • end
  • distribute (equivalent behavior to Flexbox's space-between)
  • distributeEvenly (equivalent behavior to Flexbox's space-evenly)

wrap

Sets whether items are forced onto one line or can wrap onto multiple lines.

Values:

  • wrap
  • nowrap (default)

showDividers

Boolean. Whether dividers are present between items or not.

Note: dividers are not compatible in wrapping stacks.

Note 2: the presence of a divider duplicates the gap between items.

dividerAriaRole

Sets which ARIA role will be used for the divider.

Values:

  • presentation (default)
  • separator

Structure

.Stack
├─ &.Stack--dir-[ inline | block ]-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--gap-[ none | condensed | normal | spacious ]-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--align-[ start | center | end | baseline ]-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--alignWrap-[ start | center | end | distribute | distributeEvenly ]-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--spread-[ start | center | end | distribute | distributeEvenly ]-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--wrap-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--nowrap-[ whenNarrow | whenRegular | whenWide ]
├─ &.Stack--showDividers-[ whenNarrow | whenRegular | whenWide ]
│
├─ .Stack-divider
├─ .Stack-item
│  ├─ &.Stack-item--expand-[ whenNarrow | whenRegular | whenWide ]
│  ├─ &.Stack-item--keepSize-[ whenNarrow | whenRegular | whenWide ]

Storybook

You can play with Stack in Storybook. By default it comes with _debug set to true, so you can see how the elements are positioned.

✨ Link to Storybook ✨

Can these changes ship as is?

  • Yes, this PR does not depend on additional changes. 🚢

Footnotes

  1. Discussion on Layout helper components (only available for Hubbers)

  2. Responsive API design guidelines ADR (only available for hubbers, but content will be made public in a future PR)

@changeset-bot
Copy link

changeset-bot bot commented Jul 12, 2022

🦋 Changeset detected

Latest commit: 2795894

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/css Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot temporarily deployed to Storybook Preview July 12, 2022 05:42 Inactive
src/layout/stack.scss Outdated Show resolved Hide resolved
@vdepizzol vdepizzol temporarily deployed to github-pages July 12, 2022 19:27 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 12, 2022 19:27 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 12, 2022 20:57 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 12, 2022 20:58 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 13, 2022 00:47 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 13, 2022 00:47 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 13, 2022 18:56 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 13, 2022 18:57 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 13, 2022 22:20 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 13, 2022 22:20 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 14, 2022 00:06 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 14, 2022 00:06 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 15, 2022 01:16 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 15, 2022 01:17 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 15, 2022 01:35 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 15, 2022 01:36 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 22, 2022 05:13 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 22, 2022 05:17 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 22, 2022 05:18 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 22, 2022 23:00 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 22, 2022 23:01 Inactive
@vdepizzol vdepizzol requested a review from simurai July 25, 2022 20:28
@github-actions github-actions bot temporarily deployed to Storybook Preview July 25, 2022 20:29 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 25, 2022 20:34 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 25, 2022 20:35 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 25, 2022 20:48 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 25, 2022 20:48 Inactive
@vdepizzol vdepizzol temporarily deployed to github-pages July 25, 2022 20:58 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 25, 2022 20:59 Inactive
@github-actions github-actions bot temporarily deployed to Storybook Preview July 25, 2022 21:08 Inactive
@vdepizzol vdepizzol merged commit edac9ed into main Jul 25, 2022
@vdepizzol vdepizzol deleted the Stack branch July 25, 2022 21:09
@primer-css primer-css mentioned this pull request Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants