Skip to content

Commit 98d276d

Browse files
authored
✨ NEW: Add card option img-background (#17)
1 parent cca2cfb commit 98d276d

17 files changed

+229
-20
lines changed

docs/cards.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,59 @@ Footer
7373
:::
7474
::::
7575

76-
You can also add images to the top and bottom of the card, with the `img-top` and `img-bottom` options:
76+
## Card images
7777

78-
::::{grid} 1 2 2 2
79-
:::{grid-item-card} Card Title
80-
:img-top: images/banner.png
81-
:img-bottom: images/banner.png
78+
You can also add an image as the background of a card or at the top/bottom of the card, with the `img-background`, `img-top`, `img-bottom` options:
79+
80+
:::::{grid} 2 3 3 4
81+
82+
::::{grid-item}
83+
84+
:::{card} Title
85+
:img-background: images/particle_background.jpg
86+
:class-card: sd-text-black
87+
88+
Text
89+
:::
90+
91+
::::
92+
93+
::::{grid-item-card} Title
94+
:img-top: images/particle_background.jpg
8295

8396
Header
8497
^^^
85-
Card content
98+
Content
8699
+++
87100
Footer
88-
:::
89101
::::
90102

103+
::::{grid-item-card} Title
104+
:img-bottom: images/particle_background.jpg
105+
106+
Header
107+
^^^
108+
Content
109+
+++
110+
Footer
111+
::::
112+
113+
:::::
114+
115+
`````{dropdown} Syntax
116+
:icon: code
117+
:color: light
118+
119+
````{tab-set-code}
120+
```{literalinclude} ./snippets/myst/card-images.txt
121+
:language: markdown
122+
```
123+
```{literalinclude} ./snippets/rst/card-images.txt
124+
:language: rst
125+
```
126+
````
127+
`````
128+
91129
(cards-clickable)=
92130

93131
## Clickable cards
@@ -208,6 +246,9 @@ margin
208246
text-align
209247
: Default horizontal text alignment: left, right, center or justify
210248

249+
img-background
250+
: A URI (relative file path or URL) to an image to be placed below the content.
251+
211252
img-top
212253
: A URI (relative file path or URL) to an image to be placed above the content.
213254

docs/css_classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Note, for grids the special gutter classes `sd-g-{screen-size}-{spacing}` are us
111111

112112
## Colors
113113

114-
Colors can be set using [CSS variable](./css_variables.md), they are defined for the semantic color names: primary, secondary, success, warning, danger, info, light, dark, and muted.
114+
Colors can be set using [CSS variable](./css_variables.md), they are defined for the semantic color names: `primary`, `secondary`, `success`, `warning`, `danger`, `info`, `light`, `dark`, and `muted`, and specific colors `black` and `white`.
115115

116116
- `sd-bg-{name}`
117117
- `sd-bg-text-{name}`

docs/images/banner.png

-68.7 KB
Binary file not shown.

docs/images/particle_background.jpg

38.3 KB
Loading

docs/snippets/myst/article-info.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```{article-info}
2-
:avatar: ebp-logo.png
2+
:avatar: images/ebp-logo.png
33
:avatar-link: https://executablebooks.org/
44
:avatar-outline: muted
55
:author: Executable Books

docs/snippets/myst/card-images.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
:::::{grid} 2 3 3 4
2+
3+
::::{grid-item}
4+
5+
:::{card} Title
6+
:img-background: images/particle_background.jpg
7+
:class-card: sd-text-black
8+
9+
Text
10+
:::
11+
12+
::::
13+
14+
::::{grid-item-card} Title
15+
:img-top: images/particle_background.jpg
16+
17+
Header
18+
^^^
19+
Content
20+
+++
21+
Footer
22+
::::
23+
24+
::::{grid-item-card} Title
25+
:img-bottom: images/particle_background.jpg
26+
27+
Header
28+
^^^
29+
Content
30+
+++
31+
Footer
32+
::::
33+
34+
:::::

docs/snippets/rst/article-info.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. article-info::
2-
:avatar: ebp-logo.png
2+
:avatar: images/ebp-logo.png
33
:avatar-link: https://executablebooks.org/
44
:avatar-outline: muted
55
:author: Executable Books

docs/snippets/rst/card-images.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. grid:: 2 3 3 4
2+
3+
.. grid-item::
4+
5+
.. card:: Title
6+
:img-background: images/particle_background.jpg
7+
:class-card: sd-text-black
8+
9+
Text
10+
11+
.. grid-item-card:: Title
12+
:img-top: images/particle_background.jpg
13+
14+
Header
15+
^^^
16+
Content
17+
+++
18+
Footer
19+
20+
.. grid-item-card:: Title
21+
:img-bottom: images/particle_background.jpg
22+
23+
Header
24+
^^^
25+
Content
26+
+++
27+
Footer

sphinx_design/cards.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class CardDirective(SphinxDirective):
5757
"text-align": text_align,
5858
"img-top": directives.uri,
5959
"img-bottom": directives.uri,
60+
"img-background": directives.uri,
6061
"link": directives.uri,
6162
"link-type": make_choice(["url", "any", "ref", "doc"]),
6263
"shadow": make_choice(["none", "sm", "md", "lg"]),
@@ -91,19 +92,33 @@ def create_card(
9192
)
9293
inst.set_source_info(card)
9394

95+
container = card
96+
if "img-background" in options:
97+
card.append(
98+
nodes.image(
99+
uri=options["img-background"],
100+
classes=["sd-card-img"],
101+
alt="background image",
102+
)
103+
)
104+
overlay = create_component("card-overlay", ["sd-card-img-overlay"])
105+
inst.set_source_info(overlay)
106+
card += overlay
107+
container = overlay
108+
94109
if "img-top" in options:
95110
image_top = nodes.image(
96111
"",
97112
uri=options["img-top"],
98113
alt="card-img-top",
99114
classes=["sd-card-img-top"],
100115
)
101-
card.append(image_top)
116+
container.append(image_top)
102117

103118
components = cls.split_content(inst.content, inst.content_offset)
104119

105120
if components.header:
106-
card.append(
121+
container.append(
107122
cls._create_component(
108123
inst, "header", options, components.header[0], components.header[1]
109124
)
@@ -121,10 +136,10 @@ def create_card(
121136
textnodes, _ = inst.state.inline_text(arguments[0], inst.lineno)
122137
title.extend(textnodes)
123138
body.insert(0, title)
124-
card.append(body)
139+
container.append(body)
125140

126141
if components.footer:
127-
card.append(
142+
container.append(
128143
cls._create_component(
129144
inst, "footer", options, components.footer[0], components.footer[1]
130145
)
@@ -137,7 +152,7 @@ def create_card(
137152
alt="card-img-bottom",
138153
classes=["sd-card-img-bottom"],
139154
)
140-
card.append(image_bottom)
155+
container.append(image_bottom)
141156

142157
if "link" in options:
143158
link_container = PassthroughTextElement()
@@ -162,7 +177,7 @@ def create_card(
162177
link = addnodes.pending_xref("", nodes.Text(""), **options)
163178
inst.set_source_info(link)
164179
link_container += link
165-
card.append(link_container)
180+
container.append(link_container)
166181

167182
return card
168183

sphinx_design/compiled/style.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)