Skip to content

Commit 6dfea47

Browse files
authored
add shareable links (csstools#1252)
1 parent c8197c0 commit 6dfea47

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

sites/postcss-preset-env/src/static/js/blog_color_mix_2023_03_27.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ function handleSliderInput() {
4040
debouncedReenable();
4141
}
4242

43+
function readState() {
44+
try {
45+
const hash = window.location.hash.slice(1);
46+
if (!hash) {
47+
return {};
48+
}
49+
50+
return JSON.parse(window.decodeURIComponent(window.atob(hash)));
51+
} catch (err) {
52+
console.error(err);
53+
return {};
54+
}
55+
}
56+
57+
function writeState() {
58+
try {
59+
window.location.hash = window.btoa(window.encodeURIComponent(JSON.stringify({
60+
cs: colorSpaceInput.value,
61+
im: interpolationMethodInput.value,
62+
p: colorMixPercentage.value,
63+
a: colorInputA.value,
64+
b: colorInputB.value,
65+
})));
66+
} catch (err) {
67+
console.error(err);
68+
}
69+
}
70+
4371
function renderResult() {
4472
if (
4573
!colorSpaceInput ||
@@ -55,6 +83,8 @@ function renderResult() {
5583
return;
5684
}
5785

86+
writeState();
87+
5888
switch (colorSpaceInput.value) {
5989
case 'hsl':
6090
case 'hwb':
@@ -108,3 +138,24 @@ colorMixPercentage.addEventListener('input', handleSliderInput);
108138

109139
addEventListener('change', renderResult);
110140
addEventListener('keyup', renderResult);
141+
142+
{
143+
const state = readState();
144+
if (state.cs) {
145+
colorSpaceInput.value = state.cs;
146+
}
147+
if (state.im) {
148+
interpolationMethodInput.value = state.im;
149+
}
150+
if (state.p) {
151+
colorMixPercentage.value = state.p;
152+
}
153+
if (state.a) {
154+
colorInputA.value = state.a;
155+
}
156+
if (state.b) {
157+
colorInputB.value = state.b;
158+
}
159+
160+
renderResult();
161+
}

0 commit comments

Comments
 (0)