Skip to content

Commit

Permalink
✨ feat(themes): use oklch
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Jul 13, 2024
1 parent 8d47b43 commit 532e379
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export function NavigationSkeleton() {
return (
<Skeleton
key={index}
className="h-4 max-w-24 bg-[hsla(0,0%,0%,0.3)]"
className="h-4 max-w-24 bg-[oklch(0%_0_0/30%)]"
/>
);
}
return <Skeleton key={index} className="h-6 bg-[hsla(0,0%,0%,0.3)]" />;
return <Skeleton key={index} className="h-6 bg-[oklch(0%_0_0/30%)]" />;
})}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/_layouts/app/NavigationSideBar/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function ProfileOnNavigation({ isFullWidth }: IProps) {
<DropDownMenu
ellipsis
ariaLabel="Toggle Profile Menu"
className="bg-[hsla(var(--dp-primary),0.6)] hover:bg-[hsla(var(--dp-primary),0.9)] [&_svg]:text-white"
className="bg-[oklch(0%_0_0/10%)] hover:bg-[oklch(0%_0_0/30%)] [&_svg]:text-white"
menuItems={[...constantNavigation, ...constantNavigationMenuItems].map(
({ label, action, systemIcon }) => ({
id: action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export function RenderNavigation({
marginLeft: (depth - 1) * 8,
}}
className={cn(
"mb-1 flex w-full items-center justify-start rounded-lg py-5 text-white hover:!bg-[hsla(0,0%,0%,0.1)]",
"mb-1 flex w-full items-center justify-start rounded-lg py-5 text-white hover:!bg-[oklch(0%_0_0/10%)]",
{
"bg-[hsla(0,0%,0%,0.1)]": isActive,
"bg-[oklch(0%_0_0/10%)]": isActive,
"px-3": isFullWidth,
}
)}
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/_layouts/app/NavigationSideBar/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function SideBar({ isFullWidth, setIsFullWidth }: IProps) {
: SIDE_BAR_WIDTH_VARIATIONS.collapsed,
}}
>
<div className="flex h-[50px] items-center justify-center bg-[hsla(var(--dp-primary),0.95)]">
<div className="flex h-[50px] items-center justify-center bg-[oklch(var(--dp-primary)/95%)]">
<Link href="/">
{isFullWidth ? (
<Image
Expand All @@ -111,7 +111,7 @@ export function SideBar({ isFullWidth, setIsFullWidth }: IProps) {
</Link>
</div>
<div className="flex h-[calc(100dvh-50px)] flex-col justify-between">
<ScrollArea className="h-[calc(100%-1px)] bg-[hsla(var(--dp-primary),0.95)]">
<ScrollArea className="h-[calc(100%-1px)] bg-[oklch(var(--dp-primary)/95%)]">
<ProfileOnNavigation isFullWidth={isFullWidth} />
<ViewStateMachine
loading={navigationMenuItems.isLoading}
Expand All @@ -130,7 +130,7 @@ export function SideBar({ isFullWidth, setIsFullWidth }: IProps) {
<Tooltip isOverAButton text="Use `Ctrl + B` to toggle">
<Button
variant="ghost"
className="h-9 rounded-none bg-[hsla(var(--dp-primary),0.8)] shadow-sm"
className="h-9 rounded-none bg-[oklch(var(--dp-primary)/85%)] shadow-sm"
onClick={() => setIsFullWidth(!isFullWidth)}
>
<ChevronRight
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/_layouts/guest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export function AuthLayout({ children, title, subTitle }: IProps) {
useAppTheme();
const { _ } = useLingui();

let value = 0.2;
let value = 20;

const getRandom = () => {
value -= 0.025;
return `hsla(var(--dp-primary),${value})`;
value -= 1.0;
return `oklch(var(--dp-primary)/${value}%)`;
};

return (
Expand All @@ -46,7 +46,7 @@ export function AuthLayout({ children, title, subTitle }: IProps) {
>
<rect fill="#000" width="1600" height="800" />
<g
stroke="hsla(var(--dp-primary))"
stroke="oklch(var(--dp-primary))"
strokeWidth="66.7"
strokeOpacity="0.05"
>
Expand Down
87 changes: 55 additions & 32 deletions src/frontend/_layouts/useAppTheme.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,74 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
import { useAppConfiguration } from "@/frontend/hooks/configuration/configuration.store";

function hexToHSL(H: string) {
// Convert hex to RGB first
let r = 0;
let g = 0;
let b = 0;
if (H.length === 4) {
r = `0x${H[1]}${H[1]}` as unknown as number;
g = `0x${H[2]}${H[2]}` as unknown as number;
b = `0x${H[3]}${H[3]}` as unknown as number;
} else if (H.length === 7) {
r = `0x${H[1]}${H[2]}` as unknown as number;
g = `0x${H[3]}${H[4]}` as unknown as number;
b = `0x${H[5]}${H[6]}` as unknown as number;
type ThreeNumbers = readonly [number, number, number];

function hexToRgb(hex$1: string): ThreeNumbers {
let hex = hex$1.replace("#", "");
if (hex.length === 3) {
hex = hex
.split("")
.map((c) => c + c)
.join("");
}
// Then to HSL
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return [r, g, b];
}

function rgbToXyz([r, g, b]: ThreeNumbers) {
r /= 255;
g /= 255;
b /= 255;
const cmin = Math.min(r, g, b);
const cmax = Math.max(r, g, b);
const delta = cmax - cmin;
let h = 0;
let s = 0;
let l = 0;

if (delta === 0) h = 0;
else if (cmax === r) h = ((g - b) / delta) % 6;
else if (cmax === g) h = (b - r) / delta + 2;
else h = (r - g) / delta + 4;
r = r > 0.04045 ? ((r + 0.055) / 1.055) ** 2.4 : r / 12.92;
g = g > 0.04045 ? ((g + 0.055) / 1.055) ** 2.4 : g / 12.92;
b = b > 0.04045 ? ((b + 0.055) / 1.055) ** 2.4 : b / 12.92;

h = Math.round(h * 60);
const x = (r * 0.4124564 + g * 0.3575761 + b * 0.1804375) / 0.95047;
const y = (r * 0.2126729 + g * 0.7151522 + b * 0.072175) / 1.0;
const z = (r * 0.0193339 + g * 0.119192 + b * 0.9503041) / 1.08883;

if (h < 0) h += 360;
return [x, y, z] as const;
}

function xyzToLab([x, y, z]: ThreeNumbers) {
x = x > 0.008856 ? Math.cbrt(x) : 7.787 * x + 16 / 116;
y = y > 0.008856 ? Math.cbrt(y) : 7.787 * y + 16 / 116;
z = z > 0.008856 ? Math.cbrt(z) : 7.787 * z + 16 / 116;

l = (cmax + cmin) / 2;
s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);
const l = 116 * y - 16;
const a = 500 * (x - y);
const b = 200 * (y - z);

return `${h}, ${s}%, ${l}%`;
return [l, a, b] as const;
}

function labToOklch([l, a, b]: ThreeNumbers) {
const c = Math.sqrt(a * a + b * b);
const h = Math.atan2(b, a);
const hDegrees = (h * 180) / Math.PI;
const hPositive = hDegrees < 0 ? hDegrees + 360 : hDegrees;

return [l / 100, c / 100, hPositive] as const;
}

function hexToOklch(hex: string) {
const rgb = hexToRgb(hex);
const xyz = rgbToXyz(rgb);
const lab = xyzToLab(xyz);
const [l, c, h] = labToOklch(lab);
return `${l.toFixed(3)}% ${c.toFixed(3)} ${h.toFixed(3)}`;
}

export const useAppTheme = () => {
const themeColor = useAppConfiguration("theme_color");

document.documentElement.style.setProperty(
"--app-primary",
hexToHSL(themeColor.data.primary)
hexToOklch(themeColor.data.primary)
);
};
2 changes: 1 addition & 1 deletion src/frontend/components/app/form/input/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { Story } from "@storybook/react";
import { Field, Form } from "react-final-form";
import { fakeMessageDescriptor } from "translations/fake";

import { FormCodeEditor } from "@/frontend/design-system/components/Form/CodeEditor";
import { FormFileInput } from "@/frontend/design-system/components/Form/File";
import { required } from "@/frontend/lib/validations";
import { TestProviders } from "@/tests/Provider";

import { ActionButtons } from "../../button/action";
import { DELETE_BUTTON_PROPS } from "../../button/constants";
import { FormButton } from "../../button/form";
import { FormCodeEditor } from "./code-editor";
import { FormDateInput } from "./date";
import { FormNumberInput } from "./number";
import { FormPasswordInput } from "./password";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export function FormCodeEditor(formInput: IFormCodeEditor) {
className={cn(
generateClassNames(meta),
"line-numbers",
"![&_textarea]:rounded-md !rounded-md border border-border [&_pre]:min-h-12 [&_textarea]:focus:border-primary [&_textarea]:focus:!outline-none [&_textarea]:focus:ring-1 [&_textarea]:focus:!ring-primary disabled:[&_textarea]:bg-soft",
"rounded-md border border-border p-2 shadow-sm focus:ring-1 focus:ring-primary",
{
"bg-soft": disabled,
},
styles.root
)}
>
Expand All @@ -51,7 +54,8 @@ export function FormCodeEditor(formInput: IFormCodeEditor) {
disabled={disabled}
placeholder={placeholder ? _(placeholder) : undefined}
textareaId={name}
padding={4}
textareaClassName="outline-none"
preClassName="min-h-12"
// eslint-disable-next-line tailwindcss/no-custom-classname
className="form-code-editor min-h-72 text-sm"
style={{
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/components/app/form/input/label-and-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ export function LabelAndError({ formInput, children }: IProps) {
);
}

// :eyes this is applied correctly on all forms

export const generateClassNames = (meta: FieldMetaState<any>): string =>
cn({
"ring-1 !ring-red-600": !!isFormMetaWithError(meta),
Expand Down
32 changes: 11 additions & 21 deletions src/frontend/components/app/form/input/rich-text/styles.module.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
.root :global .ql-editor {
min-height: 18em;
border: none;
border-radius: 0;
@apply rounded-none border-0 min-h-56;
}

.root :global .ql-toolbar {
display: block;
background: hsl(var(--dp-soft));
color: hsl(var(--dp-main));
border: 1px solid hsl(var(--dp-border));
border-radius: 0.25rem 0.25rem 0 0;
@apply rounded-md rounded-b-none text-main bg-soft block border border-border;
}

.root :global .ql-stroke,
.root :global .ql-fill {
color: hsl(var(--dp-main));
stroke: hsl(var(--dp-main));
@apply stroke-main text-main;
}

.root :global .ql-snow.ql-toolbar button:hover,
Expand All @@ -32,32 +25,29 @@
.root :global .ql-snow .ql-toolbar .ql-picker-item:hover,
.root :global .ql-snow.ql-toolbar .ql-picker-item.ql-selected,
.root :global .ql-snow .ql-toolbar .ql-picker-item.ql-selected {
color: hsl(var(--dp-primary));
@apply text-primary;
}

.root :global .ql-toolbar button:hover .ql-stroke {
stroke: hsl(var(--dp-primary));
@apply stroke-primary;
}

.root :global .ql-container {
background: hsl(var(--dp-base));
border: 1px solid hsl(var(--dp-border));
border-radius: 0 0 0.25rem 0.25rem;
@apply rounded-md rounded-t-none bg-base border border-border;
}

.root :global .ql-container.ql-disabled {
background-color: hsl(var(--dp-soft));
opacity: 1;
@apply bg-soft opacity-100;
}

.root :global .ql-editor.ql-blank::before {
color: hsl(var(--dp-muted));
@apply text-muted;
}

.root :global .ql-picker {
color: hsl(var(--dp-main));
@apply text-main;
}

.root :global .quill.invalid {
border: 1px solid #dc2626 !important;
.root :global .quill {
@apply rounded-md shadow-sm;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useLingui } from "@lingui/react";

import { FormInput } from "@/components/app/form/input/text";
import { FormCodeEditor } from "@/frontend/design-system/components/Form/CodeEditor";
import { FormFileInput } from "@/frontend/design-system/components/Form/File";
import { useExtendRenderFormInputProps } from "@/frontend/views/data/portal";

import { FormCodeEditor } from "../input/code-editor";
import { FormDateInput } from "../input/date";
import { FormNumberInput } from "../input/number";
import { FormPasswordInput } from "../input/password";
Expand Down
10 changes: 1 addition & 9 deletions src/frontend/components/app/roots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ export function ApplicationRoot({
}) {
return (
<ThemeProvider
themes={[
"light",
"dark",
"gruv_box",
"high_contrast",
"solarized_dark",
"solarized_light",
"nord",
]}
themes={["light", "dark"].sort((a, b) => a.localeCompare(b))}
>
<LinguiProvider translation={translation}>
<QueryProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const buttonVariants = cva(
variants: {
variant: {
default:
"bg-primary text-primary-text shadow hover:bg-[hsla(var(--dp-primary),0.85)] focus-visible:ring-primary",
"bg-primary text-primary-text shadow hover:bg-[oklch(var(--dp-primary)/85%)] focus-visible:ring-primary",
destructive:
"bg-red-100 text-red-600 shadow-sm hover:bg-red-600 hover:text-white focus-visible:ring-red-600",
soft: "bg-primary-alpha text-primary-alpha-text shadow-sm hover:bg-primary hover:text-primary-text focus-visible:ring-primary",
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/docs/_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function DocumentationRoot({ children }: { children: ReactNode }) {
<div
style={
{
"--tw-prose-code": "hsl(var(--dp-main))",
"--tw-prose-headings": "hsl(var(--dp-main))",
"--tw-prose-code": "oklch(var(--dp-tmp-text))",
"--tw-prose-headings": "oklch(var(--dp-tmp-text))",
} as CSSProperties
}
className="prose !text-main [&_pre]:!m-0"
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/views/Dashboard/Widget/_manage/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import { FormButton } from "@/components/app/button/form";
import { SoftButton } from "@/components/app/button/soft";
import { FormCodeEditor } from "@/components/app/form/input/code-editor";
import { IconInputField } from "@/components/app/form/input/icon";
import { FormSelect } from "@/components/app/form/input/select";
import { SpectrumColorInputField } from "@/components/app/form/input/spectrum";
Expand All @@ -19,7 +20,6 @@ import { RenderCode } from "@/components/app/render-code";
import { Tabs } from "@/components/app/tabs";
import { ViewStateMachine } from "@/components/app/view-state-machine";
import { Skeleton } from "@/components/ui/skeleton";
import { FormCodeEditor } from "@/frontend/design-system/components/Form/CodeEditor";
import { GRID_SPAN_OPTIONS } from "@/frontend/design-system/constants/grid";
import { useDocumentationActionButton } from "@/frontend/docs/constants";
import { WidgetScriptDocumentation } from "@/frontend/docs/scripts/widget-scripts";
Expand Down
Loading

0 comments on commit 532e379

Please sign in to comment.