Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repo): Add appearance variables to sandbox #5207

Merged
merged 10 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/witty-yaks-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
77 changes: 77 additions & 0 deletions packages/clerk-js/sandbox/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,84 @@ function addCurrentRouteIndicator(currentRoute: string) {
link.setAttribute('aria-current', 'page');
}

function appearanceVariableOptions() {
assertClerkIsLoaded(Clerk);

const resetVariablesBtn = document.getElementById('resetVariablesBtn');

const variableInputIds = [
'colorPrimary',
'colorNeutral',
'colorBackground',
'colorTextOnPrimaryBackground',
'colorDanger',
'colorSuccess',
'colorWarning',
'colorText',
'colorTextSecondary',
'colorInputText',
'colorInputBackground',
'colorShimmer',
'spacingUnit',
'borderRadius',
] as const;

const variableInputs = variableInputIds.reduce(
(acc, id) => {
const element = document.getElementById(id) as HTMLInputElement | null;
if (!element) {
throw new Error(`Could not find input element with id: ${id}`);
}
acc[id] = element;
return acc;
},
{} as Record<(typeof variableInputIds)[number], HTMLInputElement>,
);

Object.entries(variableInputs).forEach(([key, input]) => {
const savedColor = sessionStorage.getItem(key);
if (savedColor) {
input.value = savedColor;
}
});

const updateVariables = () => {
Clerk.__unstable__updateProps({
appearance: {
variables: Object.fromEntries(
Object.entries(variableInputs).map(([key, input]) => {
sessionStorage.setItem(key, input.value);
return [key, input.value];
}),
),
},
});
};

Object.values(variableInputs).forEach(input => {
input.addEventListener('change', updateVariables);
});

resetVariablesBtn?.addEventListener('click', () => {
Object.values(variableInputs).forEach(input => {
input.value = input.defaultValue;
});
updateVariables();
});

return { updateVariables };
}

(async () => {
assertClerkIsLoaded(Clerk);
const { updateVariables } = appearanceVariableOptions();

const sidebars = document.querySelectorAll('[data-sidebar]');
document.addEventListener('keydown', e => {
if (e.key === '/') {
sidebars.forEach(s => s.classList.toggle('hidden'));
}
});

const routes = {
'/': () => {
Expand Down Expand Up @@ -191,6 +267,7 @@ function addCurrentRouteIndicator(currentRoute: string) {
experimental: { commerce: true },
});
renderCurrentRoute();
updateVariables();
} else {
console.error(`Unknown route: "${route}".`);
}
Expand Down
140 changes: 136 additions & 4 deletions packages/clerk-js/sandbox/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@
};
</script>
</head>
<body class="flex min-h-full flex-col overflow-x-hidden bg-gray-50">
<div class="fixed inset-y-0 left-0 w-72 overflow-y-auto border-r border-gray-100 bg-white px-2 py-4 max-lg:hidden">
<body class="flex min-h-full flex-col overflow-x-hidden bg-gray-50 lg:has-[*[data-sidebar]:not(.hidden)]:px-72">
<div
data-sidebar
class="fixed inset-y-0 left-0 w-72 overflow-y-auto border-r border-gray-100 bg-white px-2 py-4 max-lg:hidden"
>
<header class="mb-2 flex items-center justify-center gap-x-2 border-b border-gray-100 pb-4">
<svg
viewBox="0 0 62 18"
Expand Down Expand Up @@ -277,9 +280,138 @@
</nav>
</div>

<main
class="bg-gray-25 flex h-full min-w-px flex-1 items-center justify-center overflow-y-auto overflow-x-hidden py-12 lg:pl-72"
<div
data-sidebar
class="fixed inset-y-0 right-0 w-72 overflow-y-auto border-l border-gray-100 bg-white px-2 py-4 max-lg:hidden"
>
<fieldset>
<div class="mb-2 flex items-center justify-between">
<legend class="block text-sm">Variables</legend>
<button
id="resetVariablesBtn"
class="text-sm"
>
Reset
</button>
</div>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorPrimary</span>
<input
type="color"
id="colorPrimary"
value="#2F3037"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorNeutral</span>
<input
type="color"
id="colorNeutral"
value="#000000"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorBackground</span>
<input
type="color"
id="colorBackground"
value="#ffffff"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorTextOnPrimaryBackground</span>
<input
type="color"
id="colorTextOnPrimaryBackground"
value="#ffffff"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorDanger</span>
<input
type="color"
id="colorDanger"
value="#EF4444"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorSuccess</span>
<input
type="color"
id="colorSuccess"
value="#22C543"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorWarning</span>
<input
type="color"
id="colorWarning"
value="#F36B16"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorText</span>
<input
type="color"
id="colorText"
value="#212126"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorTextSecondary</span>
<input
type="color"
id="colorTextSecondary"
value="#747686"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorInputText</span>
<input
type="color"
id="colorInputText"
value="#000000"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorInputBackground</span>
<input
type="color"
id="colorInputBackground"
value="#ffffff"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">colorShimmer</span>
<input
type="color"
id="colorShimmer"
value="#ffffff"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">spacingUnit</span>
<input
type="text"
id="spacingUnit"
value="1rem"
class="text-sm outline-none [field-sizing:content]"
/>
</label>
<label class="flex items-center justify-between border-t border-gray-100 py-2">
<span class="font-mono text-xs">borderRadius</span>
<input
type="text"
id="borderRadius"
value="0.375rem"
class="text-sm outline-none [field-sizing:content]"
/>
</label>
</fieldset>
</div>

<main class="flex h-full min-w-px flex-1 items-center justify-center overflow-y-auto overflow-x-hidden py-12">
<div
id="app"
class="container w-full p-12"
Expand Down