Skip to content

Add built-in CellAlignments presets for grid cell content positioning #22

@nicolasguelfi

Description

@nicolasguelfi

Description

st_grid cells frequently need content alignment (horizontal, vertical, or both), yet the library provides no ready-made styles for this common use case. Users must manually compose flexbox CSS strings every time:

cell_center = Style(
    "display:flex;flex-direction:column;"
    "justify-content:center;align-items:center;text-align:center;",
    "cell_center",
)

The existing building blocks (Flex, Layouts, Alignments) are close but insufficient:

  • Layouts.vertical_center_layout uses display:flex without flex-direction:column, so vertical centering doesn't work in grid cells (which stack content vertically).
  • Flex.center_justify / Flex.center_align_items are individual properties, not composable cell presets.
  • Alignments.center_align only sets text-align — no flexbox positioning.

Motivation

Grid cells are the most common context where both horizontal and vertical alignment matter. Every presentation and multi-column layout needs this. Currently, every project must define its own cell alignment styles, leading to duplicated boilerplate across all StreamTeX projects.

Proposed Solution

Add a CellAlignments class in streamtex/styles/container.py, following the same pattern as Flex, Layouts, Positions, etc.

Implementation

class CellAlignments:
    """Content alignment presets for st_grid cells.

    Each preset uses ``display:flex;flex-direction:column`` as its base,
    which aligns with ``GridController.cell()``'s vertical stacking model.
    The ``text-align`` property is included where appropriate so that
    inline text content is also aligned consistently.

    Naming convention: ``<horizontal>_<vertical>``
    - horizontal: left, center, right
    - vertical: top, middle, bottom
    """

    # ── Centered horizontally ──────────────────────────────────────
    center_top = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-start;align-items:center;text-align:center;",
        "cell_center_top",
    )
    center_middle = Style(
        "display:flex;flex-direction:column;"
        "justify-content:center;align-items:center;text-align:center;",
        "cell_center_middle",
    )
    center_bottom = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-end;align-items:center;text-align:center;",
        "cell_center_bottom",
    )

    # ── Left-aligned horizontally ──────────────────────────────────
    left_top = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-start;align-items:flex-start;",
        "cell_left_top",
    )
    left_middle = Style(
        "display:flex;flex-direction:column;"
        "justify-content:center;align-items:flex-start;",
        "cell_left_middle",
    )
    left_bottom = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-end;align-items:flex-start;",
        "cell_left_bottom",
    )

    # ── Right-aligned horizontally ─────────────────────────────────
    right_top = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-start;align-items:flex-end;text-align:right;",
        "cell_right_top",
    )
    right_middle = Style(
        "display:flex;flex-direction:column;"
        "justify-content:center;align-items:flex-end;text-align:right;",
        "cell_right_middle",
    )
    right_bottom = Style(
        "display:flex;flex-direction:column;"
        "justify-content:flex-end;align-items:flex-end;text-align:right;",
        "cell_right_bottom",
    )

    # ── Convenience alias (most common case) ───────────────────────
    center = center_middle

Integration points

  1. container.py — add the class after GridStyles
  2. Container facade — add cells = CellAlignments (line ~398)
  3. __init__.py — export CellAlignments in the container imports
  4. StxStyles shortcut (optional) — add cell_center = Container.cells.center alongside center_txt

Access patterns

# Via Container facade
Container.cells.center_middle
Container.cells.left_top

# Via StxStyles shortcut (if added)
s.cell_center

# Composable with other styles
Container.cells.center_middle + Paddings.medium_padding + BackgroundColors.azure_bg

Naming rationale

  • CellAlignments (not CellStyles) — mirrors Alignments for text, scoped to cell context
  • <h>_<v> naming — reads naturally: "center middle", "left top", "right bottom"
  • center alias → center_middle — the most frequent use case gets the shortest name

Environment

Key Value
StreamTeX 0.6.9
Python 3.13.7
OS Darwin 25.3.0 arm64
UV 0.10.2
Branch main
Commit f1a4c79

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions