Skip to content

Commit dbcef79

Browse files
not-nullptrKTibow
andauthored
feat: add animations for segmented buttons (#109)
* add cute animations for segmented buttons * simplify animation and padding --------- Co-authored-by: Kendell R <[email protected]>
1 parent 6d2f81f commit dbcef79

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/lib/buttons/SegmentedButtonItem.svelte

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
</script>
1212

1313
<label
14-
class:custom-icon={icon}
1514
for={input}
1615
class="m3-font-label-large"
17-
style="display: {display};"
16+
style="display: {display}; overflow: hidden;"
1817
{...extraOptions}
1918
>
2019
<div class="layer" />
21-
<div class="pad" />
2220
{#if icon}
2321
<div class="custom icon">
2422
<Icon {icon} />
@@ -27,8 +25,11 @@
2725
<div class="check icon">
2826
<Icon icon={iconCheck} />
2927
</div>
28+
<div class="start-pad pad" />
3029
<slot />
31-
<div class="pad" class:hidden={icon} />
30+
{#if !icon}
31+
<div class="end-pad pad" />
32+
{/if}
3233
</label>
3334

3435
<style>
@@ -60,39 +61,56 @@
6061
inset: 0;
6162
transition: all 200ms;
6263
}
63-
.icon,
64-
.pad {
64+
.icon {
65+
height: 1.125rem;
6566
transition: all 200ms;
6667
flex-shrink: 0;
68+
transform-origin: 0.563rem 0.563rem;
6769
}
6870
.icon > :global(svg) {
6971
width: 1.125rem;
7072
height: 1.125rem;
7173
}
72-
.pad {
73-
width: 0.8125rem; /* (1.125 + 0.5) / 2 */
74-
}
7574
76-
.custom-icon .icon {
77-
transition: none;
78-
}
79-
:global(input:checked) + label > .pad,
80-
label.custom-icon > .pad {
75+
.check.icon {
8176
width: 0;
77+
opacity: 0;
8278
}
83-
.icon {
84-
width: 0;
85-
height: 1.125rem;
86-
overflow: hidden;
79+
:global(input:checked) + label > .check.icon {
80+
opacity: 1;
8781
}
88-
:global(input:checked) + label > .check.icon,
89-
.custom.icon {
90-
width: 1.125rem;
91-
margin-right: 0.5rem;
82+
.custom.icon + .check.icon {
83+
rotate: -60deg;
9284
}
93-
:global(input:checked) + label > .custom.icon {
85+
:global(input:checked) + label > .custom.icon + .check.icon {
86+
rotate: 0deg;
87+
}
88+
.custom.icon {
9489
width: 0;
95-
margin-right: 0;
90+
opacity: 0;
91+
rotate: 60deg;
92+
}
93+
:global(input:not(:checked)) + label > .custom.icon {
94+
opacity: 1;
95+
rotate: 0deg;
96+
}
97+
98+
.pad {
99+
transition: all 200ms;
100+
flex-shrink: 0;
101+
}
102+
.start-pad {
103+
width: 0.8125rem;
104+
}
105+
.end-pad {
106+
width: 0.8125rem;
107+
}
108+
:global(input:checked) + label > .start-pad,
109+
.custom.icon ~ .start-pad {
110+
width: 1.625rem;
111+
}
112+
:global(input:checked) + label > .end-pad {
113+
width: 0rem;
96114
}
97115
98116
label {

0 commit comments

Comments
 (0)