Skip to content

Commit a7a8c1c

Browse files
feat(variant): add variant module to handle variants.
1 parent 89f34e3 commit a7a8c1c

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

variant/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@forward 'variant.create.function';

variant/_variant.create.function.scss

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Sass.
2+
// @use 'sass:list';
3+
4+
// Modules.
5+
@use '../list';
6+
@use '../map';
7+
8+
// Status: DONE
9+
// The `variant.create()` creates a variant built from `$value`.
10+
// @param `$value` A list or string type to create a variant.
11+
// @returns The returned value is a variant of map type where (class: value).
12+
@function create($value) {
13+
$variant: ();
14+
@if not (type-of($value) == map) {
15+
@each $value in if(list.separator($value) == comma, $value, ($value,)) {
16+
// Remove !important.
17+
$key: list.remove-value($value, !important);
18+
19+
// Set variant.
20+
$variant: map.merge($variant, ($key: $value));
21+
}
22+
}
23+
@return $variant;
24+
}
25+
26+
// Examples.
27+
// string
28+
// @debug create(right); // (right: right)
29+
30+
// space-separated list
31+
// @debug create(padding top); // (padding top: padding top)
32+
33+
// comma-separated list
34+
// @debug create((right, left)); // (right: right, left: left)
35+
36+
//
37+
// @debug create((right ('+' 15), left)); // (right: right, left: left)

variant/_variant.modify.function.scss

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Sass.
2+
@use 'sass:list';
3+
@use 'sass:map';
4+
@use 'sass:meta';
5+
6+
// Status: DONE
7+
// The `variant.modify()` function adds property suffix e.g. `color`, `radius` to the variant.
8+
// @param `$variant`
9+
// @param `$property`
10+
// @param `$suffix`
11+
// @returns
12+
@function modify($variant, $property: null, $suffix: null) {
13+
$result: ();
14+
@each $name, $side-variant in $variant {
15+
@if type-of($side-variant) == map {
16+
@each $side, $variant in $side-variant {
17+
@if type-of($variant) == map {
18+
// Modify `$property`
19+
@if $property {
20+
$name: if(meta.type-of($name) == map, map.set($name, $class, $property), ($name: $property));
21+
}
22+
@each $class, $value in $variant {
23+
@if $suffix {
24+
@if list.separator($class) == comma {
25+
$i: 1;
26+
@each $-class in $class {
27+
$class: list.set-nth($class, $i, list.join($suffix, $-class, list.separator($class)));
28+
$i: $i + 1;
29+
}
30+
} @else {
31+
$class: list.join($suffix, $class);
32+
}
33+
}
34+
35+
$result: map.deep-merge(
36+
$result,
37+
($name: ($side: ($class: if($suffix, ($suffix: $value), $value))))
38+
);
39+
}
40+
} @else {
41+
$class: $side;
42+
$side: $name;
43+
$value: $variant;
44+
$result: map.deep-merge(
45+
$result, (
46+
if($property, ((): $property), ()): (
47+
if($suffix, (if(list.length($name) > 0, ($name: $suffix), $suffix)), $name): ($class: $value)
48+
)
49+
)
50+
);
51+
52+
}
53+
}
54+
} @else {
55+
@if $property {
56+
$class: $name;
57+
$value: $side-variant;
58+
$result: map.deep-merge(
59+
$result, (
60+
((): $property): (if($suffix, $suffix, ()): ($class: $value))
61+
)
62+
);
63+
}
64+
}
65+
}
66+
@return $result;
67+
}

variant/_variant.modify.spec.scss

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Modules.
2+
@use '../property';
3+
@use '../variant';
4+
5+
// border-start-start-radius(suffix)
6+
// ----- ---- ---- -----
7+
// name side attribute
8+
9+
// Examples.
10+
// (property: variant)
11+
// @include property.variant(variant.add-suffix((
12+
// border: (medium: 20px),
13+
// outline: (medium: (unit 5))
14+
// ), radius));
15+
16+
17+
18+
19+
// (property: (side {radius}: ({radius} class: value))
20+
// @include property.variant(variant.add-suffix((
21+
// border: (
22+
// (top, right): (medium: 20px))
23+
// ), radius));
24+
25+
26+
27+
28+
// (property: (side: variant))
29+
// @include property.variant(variant.add-suffix((
30+
// (border, outline): (
31+
// (start-end, start-start): (
32+
// (medium: 20px)
33+
// ),
34+
// (top-left, top-right): (
35+
// (medium: 20px)
36+
// )
37+
// )
38+
// ), radius));
39+
40+
41+
42+
43+
// @include property.variant(variant.add-suffix((
44+
// (border, outline): (
45+
// (inline-end, inline-start): (
46+
// (primary dark: primary dark)
47+
// )
48+
// )
49+
// ), color));
50+
51+
52+
53+
54+
// @include property.variant(variant.add-suffix((
55+
// (border, outline): (
56+
// (primary dark: primary color dark)
57+
// )
58+
// ), color));
59+
60+
61+
62+
63+
// @include property.variant(variant.add-suffix((
64+
// (border, outline): (primary dark: primary color dark),
65+
// test: (secondary dark: secondary color dark),
66+
// property: (
67+
// (side-top, side-right): (primary dark: primary color dark),
68+
// (side-bottom, side-left): (secondary light: secondary color light)
69+
// )
70+
// ), color));
71+
72+
73+
74+
75+
// @include property.variant(variant.add-suffix((
76+
// border: (
77+
// (inline-start, inline-end): (
78+
// (big dotted: dotted)
79+
// )
80+
// ),
81+
// ), style));

0 commit comments

Comments
 (0)