sphinx-design

repository·main·Indexed 42 days ago

https://github.com/executablebooks/sphinx-design

A Sphinx extension for creating responsive, visually appealing web components such as grids, cards, tabs, badges, and buttons. It is the successor to sphinx-panels and supports popular themes like sphinx-book-theme, pydata-sphinx-theme, and furo. It includes built-in support for Octicon, Material Design, and FontAwesome icons.

Tokens
19.3K
Snippets
72
Records
94
Agent score
52%

What's inside sphinx-design

  1. Browser and theme compatibility for sphinx-design

    main

    Supported Browsers

    sphinx-design targets Baseline Widely Available web features. This includes evergreen browsers (Chrome, Edge, Firefox, and Safari) that have been interoperable for at least 30 months. Internet Explorer is not supported.

    Theme Support

    sphinx-design is compatible with several popular Sphinx themes, including:

    • alabaster
    • sphinx-book-theme
    • pydata-sphinx-theme
    • sphinx-rtd-theme
    • furo
  2. Control grid item width and order

    main

    You can customize how individual items behave within a grid:

    • Override Column Width: Use the columns option on a grid-item to specify how many of the 12 columns it occupies. You can use a single number (fixed) or four numbers (responsive). Use auto to size based on content.
    • Reverse Order: Use the reverse option on the grid directive to reverse the order of items. This is useful for changing visual priority between mobile (stacked) and desktop (side-by-side) views.
    ::::{grid} 2
    :::{grid-item-card}
    :columns: auto
    
    A
    :::{grid-item-card}
    :columns: 12 6 6 6
    
    B
    ::::
  3. Synchronise tabs across multiple tab-sets

    main

    You can synchronise the selection of tabs across different tab-set containers.

    1. Assign a sync key to items: Add the :sync: <key> option to each tab-item. Tabs with the same key across different sets will be selected simultaneously.
    2. Limit synchronisation to specific groups (Optional): By default, all synchronised tabs interact. To restrict synchronisation to specific sets, add the :sync-group: <group-name> option to the tab-set directive. Only tab-items within tab-sets sharing the same :sync-group: will synchronise.

    Note: Synchronisation requires JavaScript to be enabled in the browser.

    Example (MyST Markdown):

    ::::{tab-set}
    :sync-group: category
    
    :::{tab-item} Label1
    :sync: key1
    Content 1
    :::
    
    :::{tab-item} Label2
    :sync: key2
    Content 2
    :::
    ::::
    
    ::::{tab-set}
    :sync-group: category
    
    :::{tab-item} Label1
    :sync: key1
    Content 1
    :::
    
    :::{tab-item} Label2
    :sync: key2
    Content 2
    :::
    ::::
    ::::{tab-set}
    :sync-group: category
    
    :::{tab-item} Label1
    :sync: key1
    Content 1
    :::
    
    :::{tab-item} Label2
    :sync: key2
    Content 2
    :::
    ::::
  4. Stability and usage of the `sphinx_design.testing` module

    main

    The sphinx_design.testing module is a semi-public API intended for downstream extensions writing doctree regression tests.

    • Signatures: The function signatures are covered by the standard deprecation policy.
    • Output Stability: The exact output of normalize_doctree_xml is not guaranteed to be stable across docutils versions. Its purpose is specifically to smooth over changes in how docutils serializes data for regression testing.
    • Dependencies: The module has no dependency on pytest or any test extras; it can be used with only sphinx_design installed.
  5. Nest dropdowns in other components

    main

    Dropdowns are highly composable and can be nested inside other sphinx-design components, such as:

    • Admonitions: Place a dropdown inside an admonition block.
    • Parent Dropdowns: Nest a dropdown inside another dropdown.
    • Grids: Place dropdowns within grid-item elements inside a grid.
    ::::{admonition} Admonition Title
    
    :::{dropdown} Nested Dropdown
    Content
    :::
    
    ::::
  6. Nest grids for complex layouts

    main

    Grids can be nested inside grid-item directives to create complex, adaptive layouts. This allows for fine-grained control over sub-layouts within a larger grid structure.

    :::::{grid} 1 1 2 2
    :gutter: 1
    
    :::::{grid-item}
    
    ::::{grid} 1 1 1 1
    :gutter: 1
    :::{grid-item-card} Item 1.1
    Content
    :::{grid-item-card} Item 1.2
    Content
    ::::
    
    :::::
    :::::{grid-item}
    
    ::::{grid} 1 1 1 1
    :gutter: 1
    :::{grid-item-card} Item 2.1
    Content
    :::{grid-item-card} Item 2.2
    Content
    :::{grid-item-card} Item 2.3
    Content
    ::::
    
    :::::
    ::::::
  7. Understand the sphinx-design CSS architecture

    main

    The sphinx-design stylesheet is built using a two-tier plain CSS approach to avoid Node.js dependencies and enable runtime theme customization.

    1. Hand-authored component CSS: Located in style/*.css, these files contain expanded selectors for specific component areas.
    2. Generated utility CSS: These are emitted from declarative tokens defined in style/design.toml (such as color palettes, spacing scales, breakpoints, and column counts) using the tools/generate_css.py generator.

    The generator assembles these tiers in a specific ASSEMBLY order to control the CSS cascade. The final output is a single minified file: sphinx_design/static/sphinx-design.min.css.

  8. Browser support and CSS features

    main

    Styles in sphinx-design target Baseline Widely Available web features.

    For modern features like color-mix(), the library uses a graceful degradation pattern. For example, hover shades are declared with a static fallback first, followed by a color-mix(in srgb, black 15%, var(--sd-color-*)) declaration. This ensures that if a user overrides a theme color via --sd-color-*, the hover shade is re-derived at runtime in supporting browsers, while older browsers fall back to the static value.

  9. Create card carousels

    main

    The card-carousel directive creates a single row of fixed-width, scrollable cards. The directive argument must be an integer between 1 and 12, representing the number of cards to display at once. The carousel supports scroll snapping to the nearest card.

    ::::{card-carousel} 2
    
    :::{card} card 1
    content
    :::
    :::{card} card 2
    Longer content
    :::
    ::::
  10. Add tooltips to Badges

    main

    You can add a tooltip (rendered via the HTML title attribute) to any badge by appending a semicolon and a space (; ) followed by the tooltip text to the end of the badge content.

    Important Notes:

    • The tooltip is the text after the last unescaped semicolon.
    • To include a literal semicolon in the badge text, escape it with \; (e.g., {bdg}step 1; step 2`).
    • For link (bdg-link-*) or reference (bdg-ref-*) badges, you must use the explicit text <target> form for the tooltip to be recognized. A bare URL like {bdg-link-primary}https://example.com/a;b` will not be split.
    • Accessibility Warning: Tooltips are not accessible to keyboard or touch users. Do not rely on them for essential information.
    {bdg-primary}`stable ; A released, supported version`
    {bdg-link-info}`docs <https://example.com> ; Opens the documentation`
    {bdg-ref-primary}`badges <badges> ; Jump to the badges section`
  11. Override sphinx-design colors using CSS variables

    main

    All colors in sphinx-design are controlled via CSS variables. To customize the theme, create a .css file in your project's _static directory and include it in your Sphinx conf.py configuration.

    To customize semantic colors, you should define three specific variables for each color name:

    1. --sd-color-{name}: The base color.
    2. --sd-color-{name}-highlight: The color used for interaction states (like hovering over a button). Defaults to a 15% darker version of the base color.
    3. --sd-color-{name}-text: The color used for text displayed on top of that color background.

    Semantic color names available: primary, secondary, success, info, warning, danger, light, muted, and dark.

    # In your Sphinx conf.py
    html_static_path = ['_static']
    html_css_files = ['custom.css']
    /* In your _static/custom.css */
    :root {
        --sd-color-primary: #your-color;
        --sd-color-primary-highlight: #your-highlight-color;
        --sd-color-primary-text: #your-text-color;
    }