Skip to content

Commit

Permalink
chore(qwik-themes): move code internally + signal implementation (#922)
Browse files Browse the repository at this point in the history
* chore(qwik-themes): move code internally under _state folder

* chore(themes): linting

* fix(themes): use signals instead of stores

* chore(themes provider): useVisibleTask to useOnWindow

* chore(themes): move to qwik-ui/themes
  • Loading branch information
maiieul authored Aug 17, 2024
1 parent b446009 commit e4bd153
Show file tree
Hide file tree
Showing 23 changed files with 642 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Modal } from '@qwik-ui/headless';
import { Button } from '~/components/ui';
import { extractThemeCSS } from '@qwik-ui/utils';
import { LuX } from '@qwikest/icons/lucide';
import { useTheme } from 'qwik-themes';
import { useTheme } from '@qwik-ui/themes';
import globalCSS from '~/global.css?raw';
import { Highlight } from '../highlight/highlight';

Expand All @@ -12,16 +12,16 @@ export default component$(() => {

const cssThemeOutput = useSignal<string>('');

const { theme } = useTheme();
const { themeSig } = useTheme();

return (
<Modal.Root bind:show={showSig}>
<Button
onClick$={() => {
cssThemeOutput.value = extractThemeCSS(
theme === 'dark' || theme === 'light'
themeSig.value === 'dark' || themeSig.value === 'light'
? 'border-radius-0 simple primary-cyan-600 light base-slate'
: theme,
: themeSig.value,
globalCSS,
);
showSig.value = true;
Expand Down
13 changes: 8 additions & 5 deletions apps/website/src/components/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { MoonIcon } from '../icons/MoonIcon';
import { SunIcon } from '../icons/SunIcon';
import { LogoIcon, LogoWithBorders } from '../icons/logo';

import { useTheme } from 'qwik-themes';
import { useTheme } from '@qwik-ui/themes';

import { Modal } from '@qwik-ui/headless';
import { useAppState } from '~/_state/use-app-state';
import { LuX } from '@qwikest/icons/lucide';
Expand Down Expand Up @@ -126,13 +127,13 @@ export default component$(({ showVersion = false }: HeaderProps) => {
}
});

const { theme } = useTheme();
const { themeSig } = useTheme();

return (
<Modal.Root
class={cn(
'sticky top-0 z-10 flex h-16 justify-center border-b bg-background',
theme?.includes('brutalist') && 'border-b-2',
themeSig.value?.includes('brutalist') && 'border-b-2',
)}
bind:show={isSidebarOpenedSig}
>
Expand Down Expand Up @@ -219,7 +220,7 @@ export default component$(({ showVersion = false }: HeaderProps) => {
});

const DarkModeToggle = component$<PropsOf<typeof Button>>(({ ...props }) => {
const { theme, setTheme } = useTheme();
const { themeSig } = useTheme();
const switchLightDark = $((input: string | string[]): string | string[] | undefined => {
const switchWord = (word: string): string =>
word.includes('light')
Expand All @@ -237,7 +238,9 @@ const DarkModeToggle = component$<PropsOf<typeof Button>>(({ ...props }) => {
aria-label="Toggle dark mode"
size="icon"
look="ghost"
onClick$={async () => setTheme(await switchLightDark(theme || 'light'))}
onClick$={async () =>
(themeSig.value = await switchLightDark(themeSig.value || 'light'))
}
>
<div class="hidden dark:block">
<MoonIcon />
Expand Down
10 changes: 5 additions & 5 deletions apps/website/src/components/icons/logo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PropsOf, component$ } from '@builder.io/qwik';
import { cn } from '@qwik-ui/utils';
import { useTheme } from 'qwik-themes';
import { useTheme } from '@qwik-ui/themes';

export const Logo = component$<PropsOf<'svg'>>(({ ...props }) => {
return (
Expand Down Expand Up @@ -57,7 +57,7 @@ export const Logo = component$<PropsOf<'svg'>>(({ ...props }) => {
});

export const LogoWithBorders = component$<PropsOf<'svg'>>(({ ...props }) => {
const { theme } = useTheme();
const { themeSig } = useTheme();
return (
<svg
{...props}
Expand All @@ -72,7 +72,7 @@ export const LogoWithBorders = component$<PropsOf<'svg'>>(({ ...props }) => {
clip-rule="evenodd"
>
<g
class={cn(theme?.includes('light') ? 'stroke-black' : 'stroke-white')}
class={cn(themeSig.value?.includes('light') ? 'stroke-black' : 'stroke-white')}
stroke-width="8"
fill="none"
>
Expand Down Expand Up @@ -118,7 +118,7 @@ export const LogoWithBorders = component$<PropsOf<'svg'>>(({ ...props }) => {
});

export const LogoIcon = component$<PropsOf<'svg'>>(({ ...props }) => {
const { theme } = useTheme();
const { themeSig } = useTheme();
return (
<svg
{...props}
Expand All @@ -129,7 +129,7 @@ export const LogoIcon = component$<PropsOf<'svg'>>(({ ...props }) => {
xmlns="http://www.w3.org/2000/svg"
>
<g
class={cn(theme?.includes('light') ? 'stroke-black' : 'stroke-white')}
class={cn(themeSig.value?.includes('light') ? 'stroke-black' : 'stroke-white')}
stroke-width="8"
fill="none"
>
Expand Down
29 changes: 17 additions & 12 deletions apps/website/src/components/make-it-yours/make-it-yours.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
cn,
} from '@qwik-ui/utils';
import { LuSlidersHorizontal, LuX } from '@qwikest/icons/lucide';
import { useTheme } from 'qwik-themes';
import { useTheme } from '@qwik-ui/themes';

import { Button, Modal, buttonVariants } from '~/components/ui';

import { useAppState } from '~/_state/use-app-state';
Expand All @@ -19,10 +20,10 @@ import CopyCssConfig from '../copy-css-config/copy-css-config';
export default component$<PropsOf<typeof Button>>(() => {
const rootStore = useAppState();

const { theme, setTheme } = useTheme();
const { themeSig } = useTheme();

const themeComputedObjectSig = useComputed$((): ThemeConfig => {
if (!theme || theme === 'light') {
if (!themeSig.value || themeSig.value === 'light') {
return {
font: ThemeFonts.SANS,
mode: ThemeModes.LIGHT,
Expand All @@ -33,7 +34,7 @@ export default component$<PropsOf<typeof Button>>(() => {
};
}

if (theme === 'dark') {
if (themeSig.value === 'dark') {
return {
font: ThemeFonts.SANS,
mode: ThemeModes.DARK,
Expand All @@ -44,7 +45,9 @@ export default component$<PropsOf<typeof Button>>(() => {
};
}

const themeArray = Array.isArray(theme) ? theme : theme.split(' ');
const themeArray = Array.isArray(themeSig.value)
? themeSig.value
: themeSig.value.split(' ');
return {
font: themeArray[0],
mode: themeArray[1],
Expand Down Expand Up @@ -91,7 +94,7 @@ export default component$<PropsOf<typeof Button>>(() => {
themeComputedObjectSig.value.font = ThemeFonts.SANS;
}
themeComputedObjectSig.value.style = el.value;
setTheme(await themeStoreToThemeClasses$());
themeSig.value = await themeStoreToThemeClasses$();
}}
>
<option value={'simple'}>Simple</option>
Expand All @@ -114,7 +117,7 @@ export default component$<PropsOf<typeof Button>>(() => {
onClick$={async () => {
themeComputedObjectSig.value.baseColor = baseColor;

setTheme(await themeStoreToThemeClasses$());
themeSig.value = await themeStoreToThemeClasses$();
}}
class={cn(
'flex h-3 w-3 items-center justify-center rounded-none',
Expand Down Expand Up @@ -165,7 +168,7 @@ export default component$<PropsOf<typeof Button>>(() => {
size="icon"
onClick$={async () => {
themeComputedObjectSig.value.primaryColor = primaryColor;
setTheme(await themeStoreToThemeClasses$());
themeSig.value = await themeStoreToThemeClasses$();
}}
class={cn(
'h-3 w-3 rounded-none',
Expand All @@ -182,7 +185,7 @@ export default component$<PropsOf<typeof Button>>(() => {
primaryColor === 'primary-zinc-900' ||
primaryColor === 'primary-neutral-900' ||
primaryColor === 'primary-stone-900') &&
theme?.includes('dark') ? (
themeSig.value?.includes('dark') ? (
<span
class={cn(
'flex h-[10px] w-[10px] shrink-0 rounded-none',
Expand Down Expand Up @@ -439,7 +442,7 @@ export default component$<PropsOf<typeof Button>>(() => {
look="outline"
onClick$={async () => {
themeComputedObjectSig.value.borderRadius = borderRadius;
setTheme(await themeStoreToThemeClasses$());
themeSig.value = await themeStoreToThemeClasses$();
}}
class={cn('w-12', isActive && 'mb-2 border-ring')}
>
Expand All @@ -462,7 +465,7 @@ export default component$<PropsOf<typeof Button>>(() => {
themeComputedObjectSig.value.mode =
themeComputedObjectSig.value.mode?.includes('light') ? 'dark' : 'light';

setTheme(await themeStoreToThemeClasses$());
themeSig.value = await themeStoreToThemeClasses$();
}}
/>
</div>
Expand All @@ -471,7 +474,9 @@ export default component$<PropsOf<typeof Button>>(() => {
<footer class=" flex w-full justify-between gap-4">
<Button
look="ghost"
onClick$={() => setTheme(theme?.includes('dark') ? 'dark' : 'light')}
onClick$={() => {
themeSig.value = themeSig.value?.includes('dark') ? 'dark' : 'light';
}}
>
Reset
</Button>
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AppState } from './_state/app-state.type';
import { RouterHead } from './components/router-head/router-head';
import globalStyles from './global.css?inline';

import { ThemeProvider } from 'qwik-themes';
import { ThemeProvider } from '@qwik-ui/themes';

import '@fontsource-variable/inter';
import {
Expand Down
33 changes: 33 additions & 0 deletions packages/themes/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"../../.eslintrc.json",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:qwik/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["packages/themes/tsconfig.*?.json"],
"ecmaVersion": 2021,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"plugins": ["@typescript-eslint"],
"ignorePatterns": ["!**/*", "**/.storybook/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
1 change: 1 addition & 0 deletions packages/themes/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
21 changes: 21 additions & 0 deletions packages/themes/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 HiRez.io, Qwikifiers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions packages/themes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Qwik UI themes

## useTheme hook

Provides a way to access/set a theme signal to setup 'dark'/'light'/'system' or a combination of custom themes (e.g. 'dark green radius-100') programmatically.

## ThemeProvider

To set up options for the themes.

# License

MIT
28 changes: 28 additions & 0 deletions packages/themes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@qwik-ui/themes",
"version": "0.0.1",
"description": "Qwik UI themes",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/qwikifiers/qwik-ui",
"directory": "packages/themes"
},
"main": "./index.qwik.cjs",
"qwik": "./index.qwik.mjs",
"module": "./index.qwik.mjs",
"types": "./index.d.ts",
"type": "module",
"engines": {
"node": ">=16.0.0"
},
"private": false,
"scripts": {},
"peerDependencies": {
"@builder.io/qwik": "1.7.2"
},
"devDependencies": {
}
}
25 changes: 25 additions & 0 deletions packages/themes/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "themes",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/themes/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"main": "src/index.ts",
"outputPath": "dist/packages/themes",
"tsConfig": "packages/themes/tsconfig.lib.json"
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/themes/**/*.{ts,tsx,js,jsx}"]
}
}
},
"tags": []
}
2 changes: 2 additions & 0 deletions packages/themes/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { Theme } from './lib/types';
export { useTheme, ThemeProvider } from './lib/provider';
Loading

0 comments on commit e4bd153

Please sign in to comment.