Stacks Design System

repository·main·Indexed 20 days ago

https://github.com/stackexchange/stacks

Stack Overflow's design system providing a shared foundation of components, styles, and guidelines. It includes implementations for classic CSS/JS and Svelte, an icon library, and a specialized email system for creating templates and components via MJML. The system features a documentation site powered by SvelteKit and a deployable HTTP endpoint for compiling email HTML.

Tokens
131.2K
Snippets
386
Records
567
Agent score
70%

What's inside Stacks

  1. How Stacks JavaScript components work with Stimulus

    main

    Stacks JavaScript components are implemented as Stimulus controllers. Stimulus allows you to attach functionality to HTML elements using data-... attributes rather than manual event listeners.

    Key Concepts

    • Controllers: The functional unit of Stimulus. Stacks-provided controllers are always prefixed with s- (e.g., s-magic-widget).
    • Activation: You activate a controller by adding the data-controller attribute to an HTML element.

    Example of activating a controller:

    <div
        class="s-magic-widget s-magic-widget__awesome"
        data-controller="s-magic-widget"
    >
    </div>
  2. Prevent parent container collapse with .clearfix

    main

    When all child items within a parent container use float properties, the parent container will collapse because floating elements are taken out of the normal DOM order. To prevent this layout bug, apply the .clearfix class to the parent container. This uses a .clearfix() mixin to force the parent to contain its floated children.

    <div class="clearfix">
        <div class="float-left">...</div>
        <div class="float-right">...</div>
    </div>
  3. Apply styles to all descendant links using Anchor classes

    main

    If you cannot manually add a class to every <a> element (for example, when content is generated from Markdown), you can apply an anchor class to a parent container. This will apply a consistent style to all descendant links.

    Available Anchor Classes

    • .s-anchors: Base class for descendant link styling.
    • .s-anchors__default: Descendants receive default s-link styling.
    • .s-anchors__grayscale: Descendants receive gray styling.
    • .s-anchors__muted: Descendants receive muted styling.
    • .s-anchors__danger: Descendants receive destructive red styling.
    • .s-anchors__underlined: Descendants receive underline styling.
    • .s-anchors__inherit: Descendants inherit the parent element's text color.

    Behavior Rules

    • Nesting: One level of nesting is supported (e.g., an .s-anchors container inside another .s-anchors container). More than one level of nesting is not supported.
    • Overrides: An explicit .s-link class on an individual anchor element will override the styling provided by the parent .s-anchors class.
    <div class="s-anchors s-anchors__danger">
        All <a href="#">links</a> in this <a href="#">box</a> are
        <a href="#">dangerous</a>, except for <a class="s-link">this one</a> which
        uses the default color, and
        <a class="s-link s-link__muted">this muted link</a>.
    </div>
  4. Implement Toggle Buttons with accessibility

    main

    When a button switches between selected and unselected states, you must manage both the visual state and accessibility attributes:

    1. Visual State: Toggle the .is-selected class.
    2. Accessibility: Update the aria-pressed attribute to "true" or "false".
    3. Context: Use a title attribute to describe the action if appropriate.

    Example implementation logic:

    // Toggle logic
    let isSelected = toggleButton.getAttribute('aria-pressed') === 'true';
    let newState = !isSelected;
    
    toggleButton.classList.toggle('is-selected', newState);
    toggleButton.setAttribute('aria-pressed', newState.toString());
    <button class="s-btn is-selected" type="button" aria-pressed="true" title="Toggle action">Selected</button>
  5. Apply emphasis levels to colors

    main

    Emphasis determines the amount of contrast a color has against its default surface. Use emphasis levels to control how much attention an element draws.

    Emphasis Levels

    1. Bold: Highest contrast. Use this for elements that require significant attention.
      • Example background classes: bg-black-500, bg-orange-400, bg-blue-400.
      • Example text classes: fc-black.
    2. Default: Standard contrast level.
      • Example background classes: bg-black-200, bg-orange-200, bg-blue-200.
      • Example text classes: fc-black-500.
    3. Subtle: Lowest contrast. Use this for secondary or less important UI elements.
      • Example background classes: bg-black-150, bg-orange-100, bg-blue-100.
      • Example text classes: fc-black-400.
  6. Email Template Types

    main

    The system categorizes templates along a spectrum from functional to expressive:

    • Transactional: Functional emails triggered by specific events. Usually short, single-message communications with a clear call to action.
    • Newsletter: Recurring communications that may contain multiple items and various calls to action.
    • Promotional: Short, punchy, single-message communications designed to capture attention and drive engagement.
  7. Headline Variant: Highlight

    main

    The Highlight variant wraps headline text in a highlighted background that appears as one continuous block across multiple lines. This is best for short, art-directed headlines.

    Key behaviors:

    • Line Breaks: Controlled by the author using \n in the textContent prop. Each line is wrapped in its own highlighted <span> and joined with <br/>.
    • Padding: Vertical padding is applied only to the outer edges (top of the first line and bottom of the last line). Interior lines receive only horizontal padding. This ensures lines join into a single cohesive block rather than appearing as separate 'pills'.
    • Alignment: Horizontal padding is constant to ensure left and right edges align.
    • Single Line: If no \n is provided, the text is padded on all sides.
  8. Accessibility best practices for checkboxes

    main

    To ensure checkboxes are accessible to screen readers, follow these semantic HTML patterns:

    • Unique IDs: Every checkbox input must have an id attribute.
    • Label Association: Use the for attribute on the <label> element, setting its value to the id of the corresponding input.
    • Grouping: For a collection of related checkboxes, wrap them in a <fieldset> and provide a description using a <legend> element.
  9. Understand the Stack Overflow naming hierarchy

    main

    Stack Overflow uses a specific hierarchy to categorize its offerings. Understanding these distinctions ensures consistent communication across products and materials:

    • Mainbrand: The overarching brand (e.g., Stack Overflow).
    • Sub-brand: Part of the parent brand, catering to specific segments (e.g., Stack Overflow Business).
    • Product: Standalone, externally facing offerings that customers can opt into (e.g., Stack Internal, Stack Ads).
    • Feature: Functional aspects or specific actions within a product. Users must have access to the product to use a feature (e.g., Collectives).