Skip to content

Commit 34fad20

Browse files
authored
feat(radio): use mask for radio button svgs (#27)
1 parent 5305012 commit 34fad20

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/assets/scss/components/_checkbox.scss

+8-7
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,29 @@ $checkbox-line-height: 1.5 !default;
5252
&__input {
5353
width: var(--#{$prefix}checkbox-size, $checkbox-size);
5454
height: var(--#{$prefix}checkbox-size, $checkbox-size);
55-
outline: none;
5655
margin: 0;
56+
outline: none;
5757
vertical-align: top;
58-
background-position: center;
59-
background-size: contain;
6058
-webkit-appearance: none;
6159
-moz-appearance: none;
6260
appearance: none;
6361
-webkit-print-color-adjust: exact;
6462
print-color-adjust: exact;
6563
flex-shrink: 0;
6664
cursor: pointer;
67-
background-repeat: no-repeat;
6865

6966
box-shadow: var(--#{$prefix}checkbox-box-shadow, $checkbox-box-shadow);
7067
background-color: var(
7168
--#{$prefix}checkbox-background-color,
7269
$checkbox-background-color
7370
);
71+
background-position: center;
72+
background-size: contain;
73+
background-repeat: no-repeat;
74+
75+
transition: background var(--#{$prefix}transition-duration)
76+
var(--#{$prefix}transition-timing);
77+
7478
border-radius: var(
7579
--#{$prefix}checkbox-border-radius,
7680
$checkbox-border-radius
@@ -88,9 +92,6 @@ $checkbox-line-height: 1.5 !default;
8892
$checkbox-border-style
8993
);
9094

91-
transition: background var(--#{$prefix}transition-duration)
92-
var(--#{$prefix}transition-timing);
93-
9495
&--checked {
9596
background-color: var(
9697
--#{$prefix}checkbox-active-background-color,
+33-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@use "sass:list";
22

33
/* @docs */
4-
$radio-active-background-color: var(--#{$prefix}primary) !default;
4+
$radio-active-background-color: $primary !default;
55
$radio-checked-box-shadow-length: 0 0 0.5em !default;
66
$radio-checked-box-shadow-opacity: 0.8 !default;
77
$radio-box-shadow: $control-box-shadow !default;
@@ -13,18 +13,25 @@ $radio-line-height: 1.25 !default;
1313
/* @docs */
1414

1515
// filled svg calculation
16-
@function svg_radio_fill($color, $background-color: "transparent") {
17-
$start: '<svg width="100%" height="100%" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">';
18-
$content: '<g transform="matrix(4.38544,0,0,4.38544,-94.4336,-58.9876)"><path d="M32.935,13.679C39.102,13.679 44.108,18.685 44.108,24.852C44.108,31.019 39.102,36.025 32.935,36.025C26.768,36.025 21.761,31.019 21.761,24.852C21.761,18.685 26.768,13.679 32.935,13.679ZM32.798,17.05C37.105,17.05 40.601,20.546 40.601,24.852C40.601,29.158 37.105,32.655 32.798,32.655C28.492,32.655 24.996,29.158 24.996,24.852C24.996,20.546 28.492,17.05 32.798,17.05Z" style="fill:#{$color};"/><circle cx="32.84" cy="24.8" r="7.9" style="fill:#{$background-color};"/></g>';
16+
@function svg_radio_fill() {
17+
$start: '<svg width="100%" height="100%" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" fill="#e11919" stroke="#000" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;">';
18+
$content: '<g id="SVGRepo_bgCarrier" stroke-width="0"></g>' +
19+
'<g id="SVGRepo_iconCarrier">' +
20+
'<path d="M483 260c0-123-100-223-223-223S37 137 37 260s100 223 223 223S483 383 483 260Z" style="fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:64px"></path>' +
21+
'<circle cx="256" cy="256" r="144"></circle>' + "</g>";
22+
1923
$end: "</svg>";
2024

2125
@return svg-encode("#{$start}#{$content}#{$end}");
2226
}
2327

2428
// empty svg calculation
25-
@function svg_radio_empty($color) {
26-
$start: '<svg width="100%" height="100%" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">';
27-
$content: '<g transform="matrix(10.1032,0,0,10.1032,-255.068,-313.298)"><circle cx="30.195" cy="35.959" r="4.85" style="fill:#{$color};"/></g>';
29+
@function svg_radio_empty() {
30+
$start: '<svg width="100%" height="100%" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" fill="#000" stroke="#000" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;">';
31+
$content: '<g id="SVGRepo_bgCarrier" stroke-width="0"></g>' +
32+
'<g id="SVGRepo_iconCarrier">' +
33+
'<path d="M483 260c0-123-100-223-223-223S37 137 37 260s100 223 223 223S483 383 483 260Z" style="fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:64px"></path>' +
34+
"</g>";
2835
$end: "</svg>";
2936

3037
@return svg-encode("#{$start}#{$content}#{$end}");
@@ -39,27 +46,34 @@ $radio-line-height: 1.25 !default;
3946
margin-right: var(--#{$prefix}radio-margin-sibiling, $radio-margin-sibiling);
4047

4148
&__input {
49+
font-size: var(--#{$prefix}radio-font-size, inherit);
4250
width: var(--#{$prefix}radio-size, $radio-size);
4351
height: var(--#{$prefix}radio-size, $radio-size);
4452
margin: 0;
4553
vertical-align: top;
46-
background-position: center;
47-
background-size: contain;
4854
-webkit-appearance: none;
4955
-moz-appearance: none;
5056
appearance: none;
5157
-webkit-print-color-adjust: exact;
5258
print-color-adjust: exact;
53-
border-radius: 50%;
5459
cursor: pointer;
60+
box-shadow: var(--#{$prefix}radio-box-shadow, $radio-box-shadow);
61+
border-radius: 50%;
62+
background: var(
63+
--oruga-radio-active-background-color,
64+
$radio-active-background-color
65+
);
66+
mask-image: url(svg_radio_empty());
67+
background-image: none;
68+
background-position: center;
69+
background-size: contain;
5570
background-repeat: no-repeat;
71+
5672
transition: background var(--#{$prefix}transition-duration)
5773
var(--#{$prefix}transition-timing);
58-
box-shadow: var(--#{$prefix}radio-box-shadow, $radio-box-shadow);
59-
background-image: url(svg_radio_empty($radio-active-background-color));
6074

6175
&--checked {
62-
background-image: url(svg_radio_fill($radio-active-background-color));
76+
mask-image: url(svg_radio_fill());
6377
}
6478
}
6579

@@ -71,22 +85,20 @@ $radio-line-height: 1.25 !default;
7185
opacity: var(--#{$prefix}radio-disabled-opacity, $radio-disabled-opacity);
7286
}
7387

88+
// size variants
7489
@each $name, $value in $sizes {
7590
&--#{$name} {
76-
font-size: var(--#{$prefix}radio-font-size-#{$name}, $value);
91+
--#{$prefix}radio-font-size: #{$value};
92+
--#{$prefix}radio-size: #{$value};
7793
}
7894
}
95+
96+
// color variants
7997
@each $name, $pair in $colors {
8098
$color: list.nth($pair, 1);
8199
$color-invert: list.nth($pair, 2);
82100
&--#{$name} {
83-
.o-radio__input {
84-
background-image: url(svg_radio_empty($color));
85-
86-
&--checked {
87-
background-image: url(svg_radio_fill($color, $color-invert));
88-
}
89-
}
101+
--oruga-radio-active-background-color: #{$color};
90102
}
91103
}
92104
}

0 commit comments

Comments
 (0)