Skip to content

Commit 974d6ac

Browse files
Add support for grid gutters to control spacing between grid items (#693)
This PR closes #596. This was already supported in our sphinx-design CSS, just that we didn't expose it as a parameter in the `grid` shortcode, so it was _partially_ implemented (no pun intended :P). The spacing scale formula is defined here: https://github.com/scientific-python/scientific-python-hugo-theme/blob/b3056c3a6a9b71813641860e1817aab4086ab55e/assets/theme-css/sphinx-design/_spacing.scss#L1-L9 which generates the responsive `sd-g-*` classes that control the `--sd-gutter-x` and `--sd-gutter-y` variables. The gutter parameter can either take one or four integers between 0 to 5. The default is 2 for backwards compatibility. The four integers in the value, if set, correspond to the `xs`/`sm`/`md`/`lg` breakpoints. See the Bootstrap docs for more, which is what sphinx-design relies on: https://getbootstrap.com/docs/5.2/layout/grid/#grid-options --------- Co-authored-by: Stefan van der Walt <[email protected]>
1 parent 7f498fe commit 974d6ac

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

layouts/partials/_elements/grid.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{- $outline := default "false" .outline -}}
22
{{- $columns := default "auto" .columns -}}
3+
{{- $gutter := default "2" .gutter -}}
34
{{- $items := .items -}}
45
{{- if eq $outline "true" -}}
56
{{- $outline = "sd-border-1" -}}
@@ -13,7 +14,19 @@
1314
{{- $sm := index $columns 1 }}
1415
{{- $md := index $columns 2 }}
1516
{{- $lg := index $columns 3 }}
16-
<div class="sd-row sd-row-cols-{{ $xs }} sd-row-cols-xs-{{ $xs }} sd-row-cols-sm-{{ $sm }} sd-row-cols-md-{{ $md }} sd-row-cols-lg-{{ $lg }} sd-g-2 sd-g-xs-{{ $xs }} sd-g-sm-{{ $sm }} sd-g-md-{{ $md }} sd-g-lg-{{ $lg }}">
17+
{{- $gutterParts := split $gutter " " -}}
18+
{{- $gXs := index $gutterParts 0 -}}
19+
{{- $gSm := $gXs -}}
20+
{{- $gMd := $gXs -}}
21+
{{- $gLg := $gXs -}}
22+
{{- if and (gt (len $gutterParts) 1) (ne (len $gutterParts) 4) -}}
23+
{{- errorf "grid gutter must be either 1 or 4 values (but got %d): %q" (len $gutterParts) $gutter -}}
24+
{{- else if eq (len $gutterParts) 4 -}}
25+
{{- $gSm = index $gutterParts 1 -}}
26+
{{- $gMd = index $gutterParts 2 -}}
27+
{{- $gLg = index $gutterParts 3 -}}
28+
{{- end -}}
29+
<div class="sd-row sd-row-cols-{{ $xs }} sd-row-cols-xs-{{ $xs }} sd-row-cols-sm-{{ $sm }} sd-row-cols-md-{{ $md }} sd-row-cols-lg-{{ $lg }} sd-g-{{ $gXs }} sd-g-xs-{{ $gXs }} sd-g-sm-{{ $gSm }} sd-g-md-{{ $gMd }} sd-g-lg-{{ $gLg }}">
1730
{{- end }}
1831
{{- range $key, $item := $items -}}
1932
{{- if eq $item.type "card" }}

layouts/shortcodes/grid.html

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,83 @@
118118

119119
{{< /grid >}}
120120

121+
Here's a grid with [gutters](https://sphinx-design.readthedocs.io/en/pydata-theme/grids.html#controlling-spacing-between-items).
122+
A single value applies to all breakpoints.
123+
124+
{{< grid columns="1 2 3 4" gutter="1" >}}
125+
126+
[[item]]
127+
type = 'card'
128+
body = 'Tight spacing'
129+
130+
[[item]]
131+
type = 'card'
132+
body = 'Tight spacing'
133+
134+
[[item]]
135+
type = 'card'
136+
body = 'Tight spacing'
137+
138+
[[item]]
139+
type = 'card'
140+
body = 'Tight spacing'
141+
142+
{{< /grid >}}
143+
144+
Here's a grid with no gutters, which means no spacing between items.
145+
146+
{{< grid columns="2 2 2 2" gutter="0" >}}
147+
148+
[[item]]
149+
type = 'card'
150+
body = 'No spacing'
151+
152+
[[item]]
153+
type = 'card'
154+
body = 'No spacing'
155+
156+
{{< /grid >}}
157+
158+
Here's a grid with responsive gutters (i.e., "xs sm md lg" are set).
159+
160+
{{< grid columns="1 2 2 3" gutter="1 1 3 4" >}}
161+
162+
[[item]]
163+
type = 'card'
164+
body = 'Responsive gutter: 0.25rem on mobile, 1rem on tablet, 1.5rem on desktop'
165+
166+
[[item]]
167+
type = 'card'
168+
body = 'Responsive gutter: 0.25rem on mobile, 1rem on tablet, 1.5rem on desktop'
169+
170+
[[item]]
171+
type = 'card'
172+
body = 'Responsive gutter: 0.25rem on mobile, 1rem on tablet, 1.5rem on desktop'
173+
174+
{{< /grid >}}
175+
176+
And here's a grid with a large gutter, for more spacious layouts.
177+
178+
{{< grid columns="2 2 2 2" gutter="5" >}}
179+
180+
[[item]]
181+
type = 'card'
182+
body = 'Large spacing (3rem)'
183+
184+
[[item]]
185+
type = 'card'
186+
body = 'Large spacing (3rem)'
187+
188+
{{< /grid >}}
189+
190+
The parameters are based on sphinx-design system grid utilities, which is in-turn based on Bootstrap grid system. They are as follows:
191+
- 0: 0 (no spacing)
192+
- 1: 0.25rem
193+
- 2: 0.5rem (default)
194+
- 3: 1rem
195+
- 4: 1.5rem
196+
- 5: 3rem
197+
121198
*/}}
122199

123200
{{- $items := "" -}}

0 commit comments

Comments
 (0)