GridLayout
Responsive CSS Grid helper with auto-fit columns and an optional cursor-following item spotlight.
Usage
Loading demo...
Card Grid
<template>
<TxGridLayout min-item-width="240px" gap="16px" :max-columns="3">
<article v-for="card in cards" :key="card.id" class="tx-grid-layout__item p-4">
<h3>{{ card.title }}</h3>
<p>{{ card.description }}</p>
</article>
</TxGridLayout>
</template>
Static Grid
Turn off interactive for dense tables, virtualized content, or any grid where pointer movement should not mutate child inline styles.
<template>
<TxGridLayout :interactive="false" min-item-width="180px" gap="12px">
<div v-for="metric in metrics" :key="metric.name" class="rounded-xl border p-3">
{{ metric.name }}
</div>
</TxGridLayout>
</template>
Best Practices
- Use
TxGridLayoutfor repeated peer cards. UseTxFlexorTxStackfor one-dimensional alignment. - Add
.tx-grid-layout__itemonly when you want the built-in background, radius, cursor, and spotlight style. - Disable
interactivefor very large grids to avoid per-mousemove style updates across many children. - Keep
minItemWidthaligned with the card’s real minimum readable width; do not use it as a spacing hack. - Avoid nesting interactive grids inside other pointer-heavy surfaces.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
minItemWidth | string | '300px' | Minimum width used by the auto-fit grid columns. |
gap | string | '1.5rem' | CSS gap between grid items. |
maxColumns | number | 4 | Fixed column count used on wide screens (>= 1400px). |
interactive | boolean | true | Enables cursor-following spotlight updates for .tx-grid-layout__item children. |
Slots
| Slot | Props | Description |
|---|---|---|
default | - | Grid item content. Add .tx-grid-layout__item to children that should receive the built-in card style and spotlight variables. |
Events
No public events are emitted.
Exposed Methods
No public instance methods are exposed.
CSS Variables
| Variable | Source | Description |
|---|---|---|
--tx-grid-gap | gap | Root grid gap. |
--tx-grid-min-width | minItemWidth | Minimum column width. |
--tx-grid-max-columns | maxColumns | Wide-screen column count. |
--tx-grid-op | mouse state | Spotlight opacity on each item. |
--tx-grid-x / --tx-grid-y | mouse state | Pointer position relative to each item. |
Overview
- The root is a block
divwith CSS Grid layout. - Columns use
repeat(auto-fit, minmax(minItemWidth, 1fr))by default. - At viewport widths
>= 1400px, columns switch torepeat(maxColumns, 1fr). gap,minItemWidth, andmaxColumnsare written as CSS variables on the root.- The spotlight effect is applied only to descendants with class
.tx-grid-layout__item. - When
interactive=true, mouse movement updates each.tx-grid-layout__itemwith--tx-grid-op,--tx-grid-x, and--tx-grid-yinline style variables. - Mouse leave sets
--tx-grid-opback to0. - When
interactive=false, pointer movement and mouse leave handlers return without mutating child styles.
Technologies
- Reviewed against
packages/tuffex/packages/components/src/grid-layout/index.ts,TxGridLayout.vue, andgrid-layout.test.ts. - Spotlight variables are written only to descendants with
.tx-grid-layout__item; ordinary slotted children remain untouched. interactive=falseprevents pointer handlers from mutating child inline styles, which is important for large or virtualized grids.- Component source:
packages/tuffex/packages/components/src/grid-layout/src/TxGridLayout.vue. - Types:
packages/tuffex/packages/components/src/grid-layout/index.ts. - Verified coverage: Coverage:
packages/tuffex/packages/components/src/grid-layout/__tests__/grid-layout.test.tsverifies default grid variables and slot content, prop-driven variable updates, and spotlight variables mutating only while interactive.
查看源码
packages/tuffex/packages/components/src/grid-layout/index.ts