Skip to content

Commit 85e89ab

Browse files
committed
update readme and layout grid logic
1 parent 452a682 commit 85e89ab

File tree

3 files changed

+107
-100
lines changed

3 files changed

+107
-100
lines changed

README.md

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
- [Scripts](#scripts)
3535
- [Grid Overlay Toggle](#grid-overlay-toggle)
3636
- [Breakpoints Configuration](#breakpoints-configuration)
37+
- [Layout Root (Dynamic SCSS Variables)](#layout-root-dynamic-scss-variables)
3738
- [Grid Calculations](#grid-calculations)
38-
- [Grid Layout (SCSS Variables)](#grid-layout-scss-variables)
39+
- [Layout SCSS](#layout-scss)
3940
- [Grid Overlay SCSS](#grid-overlay-scss)
4041
- [Component Structure](#component-structure)
4142
- [GridOverlayToggle Component](#gridoverlaytoggle-component)
@@ -219,37 +220,7 @@ The grid system uses the following breakpoints (defined in `tailwind.config.cjs`
219220
}
220221
```
221222

222-
### Grid Calculations
223-
224-
With a hardcoded variable of `--layout-gap: theme('spacing.4')` (16px), the grid remains fully responsive and divisible by multiples of 2.
225-
226-
- **Responsive Columns & Padding:**
227-
228-
- **xs** (`320px`): `--layout-cols: 4;` / `--layout-padding: theme('spacing.4')` (16px)
229-
- **sm** (`480px`): `--layout-cols: 4;` / `--layout-padding: theme('spacing.4')` (16px)
230-
- **md** (`592px`): `--layout-cols: 6;` / `--layout-padding: theme('spacing.6')` (24px)
231-
- **lg** (`784px`): `--layout-cols: 8;` / `--layout-padding: theme('spacing.8')` (32px)
232-
- **xl** (`976px`): `--layout-cols: 10;` / `--layout-padding: theme('spacing.10')` (40px)
233-
- **2xl** (`1168px`): `--layout-cols: 12;` / `--layout-padding: theme('spacing.12')` (48px)
234-
- **3xl** (`1360px`): `--layout-cols: 14;` / `--layout-padding: theme('spacing.14')` (56px)
235-
- **4xl** (`1552px`): `--layout-cols: 16;` / `--layout-padding: theme('spacing.16')` (64px)
236-
237-
- **Clamp Layout Column Width:**
238-
239-
```scss
240-
--layout-col-width: clamp(
241-
60px,
242-
calc(
243-
(100vw - (2 * var(--layout-padding)) - ((var(--layout-cols) - 1) * var(--layout-gap))) /
244-
var(--layout-cols)
245-
),
246-
124px
247-
);
248-
```
249-
250-
This expression dynamically adjusts the column width based on the current `--layout-padding` and `--layout-cols` values at each breakpoint.
251-
252-
### Grid Layout (SCSS Variables)
223+
### Layout Root (Dynamic SCSS Variables)
253224

254225
The grid system relies on dynamic scss variables updated at the above breakpoints to coincide with a responsive layout that is always divisible by multiples of 2. These dynamic, responsive, variables are used in both the `.layout` and `[data-grid-overlay]` scss selectors.
255226

@@ -304,11 +275,39 @@ The grid system relies on dynamic scss variables updated at the above breakpoint
304275
}
305276
```
306277

307-
### Grid Overlay SCSS
278+
### Grid Calculations
308279

309-
A purely css driven grid overlay that coincides with the layout class and it's breakpoints. When the data-overlay-grid is set to active, the grid overlay fades into visibility.
280+
With a hardcoded variable of `--layout-gap: theme('spacing.4')` (16px), the grid remains fully responsive and divisible by multiples of 2.
310281

311-
- **Layout**
282+
- **Responsive Columns & Padding:**
283+
284+
- **xs** (`320px`): `--layout-cols: 4;` / `--layout-padding: theme('spacing.4')` (16px)
285+
- **sm** (`480px`): `--layout-cols: 4;` / `--layout-padding: theme('spacing.4')` (16px)
286+
- **md** (`592px`): `--layout-cols: 6;` / `--layout-padding: theme('spacing.6')` (24px)
287+
- **lg** (`784px`): `--layout-cols: 8;` / `--layout-padding: theme('spacing.8')` (32px)
288+
- **xl** (`976px`): `--layout-cols: 10;` / `--layout-padding: theme('spacing.10')` (40px)
289+
- **2xl** (`1168px`): `--layout-cols: 12;` / `--layout-padding: theme('spacing.12')` (48px)
290+
- **3xl** (`1360px`): `--layout-cols: 14;` / `--layout-padding: theme('spacing.14')` (56px)
291+
- **4xl** (`1552px`): `--layout-cols: 16;` / `--layout-padding: theme('spacing.16')` (64px)
292+
293+
- **Clamp Layout Column Width:**
294+
295+
```scss
296+
--layout-col-width: clamp(
297+
60px,
298+
calc(
299+
(100vw - (2 * var(--layout-padding)) - ((var(--layout-cols) - 1) * var(--layout-gap))) /
300+
var(--layout-cols)
301+
),
302+
124px
303+
);
304+
```
305+
306+
This expression dynamically adjusts the column width based on the current `--layout-padding` and `--layout-cols` values at each breakpoint.
307+
308+
### Layout SCSS
309+
310+
A responsive grid container that adapts to the dynamic variables defined in the layout.scss `:root`. It is utilized on the main element in the server rendered `layout.tsx` found in the app directory.
312311

313312
```scss
314313
.layout {
@@ -330,36 +329,44 @@ A purely css driven grid overlay that coincides with the layout class and it's b
330329
@apply my-auto;
331330
}
332331
}
332+
333+
//... &[data-grid-overlay] logic below
333334
}
334335
```
335336

336-
- **[Data-Grid-Overlay]**
337-
338-
```scss
339-
[data-grid-overlay] {
340-
@apply mx-auto bg-repeat-x z-10;
337+
### Grid Overlay SCSS
341338

342-
width: calc(
343-
var(--layout-cols) * var(--layout-col-width) + (var(--layout-cols) - 1) * var(--layout-gap)
344-
);
339+
A purely css driven grid overlay that coincides with the layout class and it's breakpoints. When the data-overlay-grid is set to active, the grid overlay fades into visibility. It is dependent on being used on the same element as the `.layout` class. This is utilized on the main element in the server rendered `layout.tsx` found in the app directory.
345340

346-
background-image: repeating-linear-gradient(
347-
to right,
348-
rgba(255, 0, 0, 0.2) 0,
349-
rgba(255, 0, 0, 0.2) var(--layout-col-width),
350-
transparent var(--layout-col-width),
351-
transparent calc(var(--layout-col-width) + var(--layout-gap))
352-
);
353-
background-size: calc(var(--layout-col-width) + var(--layout-gap)) 100%;
354-
opacity: 0;
355-
visibility: hidden;
356-
transition:
357-
opacity 0.3s ease-in-out,
358-
visibility 0.3s ease-in-out;
359-
360-
&[data-grid-overlay='active'] {
361-
opacity: 1;
362-
visibility: visible;
341+
```scss
342+
.layout {
343+
// ...layout logic above
344+
345+
&[data-grid-overlay] {
346+
@apply mx-auto bg-repeat-x z-10;
347+
348+
width: calc(
349+
var(--layout-cols) * var(--layout-col-width) + (var(--layout-cols) - 1) * var(--layout-gap)
350+
);
351+
352+
background-image: repeating-linear-gradient(
353+
to right,
354+
rgba(255, 0, 0, 0.2) 0,
355+
rgba(255, 0, 0, 0.2) var(--layout-col-width),
356+
transparent var(--layout-col-width),
357+
transparent calc(var(--layout-col-width) + var(--layout-gap))
358+
);
359+
background-size: calc(var(--layout-col-width) + var(--layout-gap)) 100%;
360+
opacity: 0;
361+
visibility: hidden;
362+
transition:
363+
opacity 0.3s ease-in-out,
364+
visibility 0.3s ease-in-out;
365+
366+
&[data-grid-overlay='active'] {
367+
opacity: 1;
368+
visibility: visible;
369+
}
363370
}
364371
}
365372
```
@@ -372,35 +379,35 @@ A purely css driven grid overlay that coincides with the layout class and it's b
372379
- Displays the active breakpoint label
373380
- Shows a grid icon with an active/inactive state
374381

375-
- **GridOverlay**: A purely CSS-driven overlay that:
382+
- **[data-grid-overlay]**: A purely CSS-driven overlay that:
376383
- Uses CSS variables (`--layout-cols`, `--layout-col-width`, `--layout-gap`, etc.) to match the current layout
377384
- Renders semi‑transparent columns across the viewport
378-
- Transitions in/out when `data-grid-overlay="active"`
385+
- Transitions in/out when `data-grid-overlay="active"` is set by the GridOverlayToggle
379386

380387
### GridOverlayToggle Component
381388

382389
A client side button that triggers the css overlay grid. Updates the main element data-grid-overlay with the active state.
383390

384391
```tsx
385392
const [active, setActive] = useState(false);
386-
const mainRef = useRef<HTMLElement | null>(null);
393+
const overlayRef = useRef<HTMLElement | null>(null);
387394

388-
// Grab <main> once on mount
395+
// Grab the element with [data-grid-overlay] once on mount
389396
useEffect(() => {
390-
mainRef.current = document.querySelector('main');
397+
overlayRef.current = document.querySelector('[data-grid-overlay]');
391398
}, []);
392399

393-
// Update data-grid-overlay on <main> when `active` changes
400+
// Update the attribute whenever `active` changes
394401
useEffect(() => {
395-
if (mainRef.current) {
396-
mainRef.current.setAttribute('data-grid-overlay', active ? 'active' : '');
402+
if (overlayRef.current) {
403+
overlayRef.current.setAttribute('data-grid-overlay', active ? 'active' : '');
397404
}
398405
}, [active]);
399406
```
400407

401408
### Implementation
402409

403-
Add the grid system to your server render layout:
410+
Add the grid system to your server render layout found in `layout.tsx` within the app directory:
404411

405412
```tsx
406413
<body>

src/components/GridOverlayToggle/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ const breakpoints = [
1616

1717
const GridOverlayToggle: React.FC = () => {
1818
const [active, setActive] = useState(false);
19-
const mainRef = useRef<HTMLElement | null>(null);
19+
const overlayRef = useRef<HTMLElement | null>(null);
2020

21-
// Get main element reference once on mount
21+
// Grab the element with [data-grid-overlay] once on mount
2222
useEffect(() => {
23-
mainRef.current = document.querySelector('main');
23+
overlayRef.current = document.querySelector('[data-grid-overlay]');
2424
}, []);
2525

26-
// Toggle grid overlay
26+
// Update the attribute whenever `active` changes
2727
useEffect(() => {
28-
if (mainRef.current) {
29-
mainRef.current.setAttribute('data-grid-overlay', active ? 'active' : '');
28+
if (overlayRef.current) {
29+
overlayRef.current.setAttribute('data-grid-overlay', active ? 'active' : '');
3030
}
3131
}, [active]);
3232

src/styles/utilities/layout.scss

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,31 @@
6969
@apply my-auto;
7070
}
7171
}
72-
}
73-
74-
[data-grid-overlay] {
75-
@apply mx-auto bg-repeat-x z-10;
76-
77-
width: calc(
78-
var(--layout-cols) * var(--layout-col-width) + (var(--layout-cols) - 1) * var(--layout-gap)
79-
);
8072

81-
background-image: repeating-linear-gradient(
82-
to right,
83-
rgba(255, 0, 0, 0.2) 0,
84-
rgba(255, 0, 0, 0.2) var(--layout-col-width),
85-
transparent var(--layout-col-width),
86-
transparent calc(var(--layout-col-width) + var(--layout-gap))
87-
);
88-
background-size: calc(var(--layout-col-width) + var(--layout-gap)) 100%;
89-
opacity: 0;
90-
visibility: hidden;
91-
transition:
92-
opacity 0.3s ease-in-out,
93-
visibility 0.3s ease-in-out;
94-
95-
&[data-grid-overlay='active'] {
96-
opacity: 1;
97-
visibility: visible;
73+
&[data-grid-overlay] {
74+
@apply mx-auto bg-repeat-x z-10;
75+
76+
width: calc(
77+
var(--layout-cols) * var(--layout-col-width) + (var(--layout-cols) - 1) * var(--layout-gap)
78+
);
79+
80+
background-image: repeating-linear-gradient(
81+
to right,
82+
rgba(255, 0, 0, 0.2) 0,
83+
rgba(255, 0, 0, 0.2) var(--layout-col-width),
84+
transparent var(--layout-col-width),
85+
transparent calc(var(--layout-col-width) + var(--layout-gap))
86+
);
87+
background-size: calc(var(--layout-col-width) + var(--layout-gap)) 100%;
88+
opacity: 0;
89+
visibility: hidden;
90+
transition:
91+
opacity 0.3s ease-in-out,
92+
visibility 0.3s ease-in-out;
93+
94+
&[data-grid-overlay='active'] {
95+
opacity: 1;
96+
visibility: visible;
97+
}
9898
}
9999
}

0 commit comments

Comments
 (0)