Skip to content

Commit 9a402eb

Browse files
committed
Add examples for when you may want to use mixins or each
1 parent 2f0e766 commit 9a402eb

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

src/scss/_examples.scss

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Sketches of Sass features (@mixin, @each) that could DRY up patterns
3+
* repeated across the reference partials in this folder. This file is
4+
* NOT forwarded by theme.scss and is not part of any build in this
5+
* template — it exists purely as a discoverable reference for anyone
6+
* extending this template who wants to go beyond plain nested CSS.
7+
*
8+
* Compiling it standalone (e.g. `sass _examples.scss`) works and
9+
* produces real CSS, so you can see exactly what each example expands
10+
* to, but nothing here is wired into theme.css, lint.yml, or release.yml.
11+
*/
12+
13+
@use "sass:map";
14+
15+
// ---------------------------------------------------------------------
16+
// 1. @mixin — the hover-media pattern
17+
// ---------------------------------------------------------------------
18+
// Nearly every partial repeats this shape (e.g. _status-bar.scss,
19+
// _canvas.scss, _properties.scss...):
20+
//
21+
// .some-selector {
22+
// ...
23+
// @media (hover: hover) {
24+
// &:hover {
25+
// ...
26+
// }
27+
// }
28+
// }
29+
//
30+
// A small mixin collapses that boilerplate to one line at each call site.
31+
32+
@mixin hover {
33+
@media (hover: hover) {
34+
&:hover {
35+
@content;
36+
}
37+
}
38+
}
39+
40+
// Usage example:
41+
.example-hover-button {
42+
color: var(--text-muted);
43+
@include hover {
44+
color: var(--text-normal);
45+
}
46+
}
47+
// Expands to the same CSS as writing the full @media/&:hover block by hand.
48+
49+
// ---------------------------------------------------------------------
50+
// 2. @mixin — the RTL-flag selector group
51+
// ---------------------------------------------------------------------
52+
// _platform.scss repeats ".mod-rtl, .is-rtl, .rtl { ... }" (and the same
53+
// trio prefixed onto descendant selectors) several times over to cover
54+
// every way Obsidian marks right-to-left content. A mixin can generate
55+
// that selector list from a single call.
56+
57+
@mixin rtl {
58+
.mod-rtl &,
59+
.is-rtl &,
60+
.rtl & {
61+
@content;
62+
}
63+
}
64+
65+
// Usage example:
66+
.example-icon {
67+
transform: scale(1, 1);
68+
@include rtl {
69+
transform: scale(-1, 1);
70+
}
71+
}
72+
73+
// ---------------------------------------------------------------------
74+
// 3. @each — generating the callout color/icon variants from a map
75+
// ---------------------------------------------------------------------
76+
// _callouts.scss hand-writes one rule per callout type, e.g.:
77+
//
78+
// &[data-callout="info"] {
79+
// --callout-color: var(--callout-info);
80+
// --callout-icon: lucide-info;
81+
// }
82+
// .callout[data-callout="warning"],
83+
// .callout[data-callout="caution"],
84+
// .callout[data-callout="attention"] {
85+
// --callout-color: var(--callout-warning);
86+
// --callout-icon: lucide-alert-triangle;
87+
// }
88+
//
89+
// ...repeated for a dozen-plus types, several with multiple aliases.
90+
// A Sass map plus @each generates the same rules from data instead of
91+
// hand-copied blocks, and makes adding a new callout type or alias a
92+
// one-line change.
93+
94+
$example-callout-types: (
95+
"info": (color: info, icon: lucide-info, aliases: ()),
96+
"todo": (color: todo, icon: lucide-check-circle-2, aliases: ()),
97+
"warning": (color: warning, icon: lucide-alert-triangle, aliases: (caution, attention)),
98+
"success": (color: success, icon: lucide-check, aliases: (check, done)),
99+
);
100+
101+
.example-callout {
102+
@each $type, $props in $example-callout-types {
103+
$aliases: map.get($props, aliases);
104+
$selectors: "&[data-callout=\"#{$type}\"]";
105+
@each $alias in $aliases {
106+
$selectors: "#{$selectors}, &[data-callout=\"#{$alias}\"]";
107+
}
108+
#{$selectors} {
109+
--callout-color: var(--callout-#{map.get($props, color)});
110+
--callout-icon: #{map.get($props, icon)};
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)