Skip to content

Commit

Permalink
feat(nextra-theme-docs): primary color.lightness theme config (#3592)
Browse files Browse the repository at this point in the history
* feat(nextra-theme-docs): primary lightness

* docs: add lightness to slider tester

* chore: prettier

* chore: lint is angry with order

* docs: reword theme colour description, and add callout for white/grey example

* chore: create changeset
  • Loading branch information
wayne-shih authored Oct 29, 2024
1 parent a80f3ce commit d755012
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-years-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextra-theme-docs": feat
---

feat(nextra-theme-docs): add new theme config option `color.lightness`
22 changes: 22 additions & 0 deletions docs/components/_slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,25 @@ export function Saturation() {
</div>
)
}

export function Lightness() {
return (
<div className="flex h-6 items-center gap-2">
<input
type="range"
min="0"
max="100"
step="1"
onChange={e => {
const value = `${e.target.value}%`
e.target.nextSibling!.textContent = value
document.documentElement.style.setProperty(
'--nextra-primary-lightness',
value
)
}}
/>
<label className="text-sm text-gray-500 w-14" />
</div>
)
}
47 changes: 35 additions & 12 deletions docs/pages/docs/docs-theme/theme-configuration.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hue, Saturation } from '@components/_slider'
import { Hue, Lightness, Saturation } from '@components/_slider'
import { Screenshot } from 'components/screenshot'
import { Callout } from 'nextra/components'
import logoImage from 'public/assets/docs/logo.png'
Expand Down Expand Up @@ -146,25 +146,48 @@ Customize the theme behavior of the website.

### Theme Color

You can adjust the theme color of the website by setting primary hue and
saturation values for light and dark themes.
You can adjust the theme color of the website by setting primary hue, saturation
and lightness (HSL) values for light and dark themes.

<Table>

| | | |
| --------------------- | ------------------------------------------- | ----------------------------------------- |
| color.hue | `number \| { dark: number; light: number }` | The hue of the primary theme color |
| color.saturation | `number \| { dark: number; light: number }` | The saturation of the primary theme color |
| backgroundColor.dark | `string` in format `RRR,GGG,BBB` | Background color for light theme |
| backgroundColor.light | `string` in format `RRR,GGG,BBB` | Background color for dark theme |
| | | |
| --------------------- | ------------------------------------------- | --------------------------------------------------- |
| color.hue | `number \| { dark: number; light: number }` | The hue of the primary theme color (0 - 360) |
| color.saturation | `number \| { dark: number; light: number }` | The saturation of the primary theme color (0 - 100) |
| color.lightness | `number \| { dark: number; light: number }` | The lightness of the primary theme color (0 - 100) |
| backgroundColor.dark | `string` in format `RRR,GGG,BBB` | Background color for light theme |
| backgroundColor.light | `string` in format `RRR,GGG,BBB` | Background color for dark theme |

</Table>

Try it out for this website:

| Hue | Saturation |
| ------- | -------------- |
| <Hue /> | <Saturation /> |
| Hue (H) | Saturation (S) | Lightness (L) |
| ------- | -------------- | ------------- |
| <Hue /> | <Saturation /> | <Lightness/> |

<Callout>

You can adjust the lightness independently for dark or light mode to increase
legibility. For example, to have a neutral primary color you can set the primary
colour to be white `HSL(0, 0%, 100%)` on dark theme and gray `HSL(0, 0%, 50%)`
for light theme.

```jsx filename="theme.config.jsx"
export default {
color: {
hue: 0,
saturation: 0,
lightness: {
dark: 100,
light: 50
}
}
}
```

</Callout>

## Navbar

Expand Down
8 changes: 6 additions & 2 deletions packages/nextra-theme-docs/src/components/head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export function Head(): ReactElement {
typeof themeConfig.head === 'function'
? themeConfig.head({})
: themeConfig.head
const { hue, saturation } = themeConfig.color
const { hue, saturation, lightness } = themeConfig.color
const { dark: darkHue, light: lightHue } =
typeof hue === 'number' ? { dark: hue, light: hue } : hue
const { dark: darkSaturation, light: lightSaturation } =
typeof saturation === 'number'
? { dark: saturation, light: saturation }
: saturation
const { dark: darkLightness, light: lightLightness } =
typeof lightness === 'number'
? { dark: lightness, light: lightness }
: lightness

const bgColor = themeConfig.backgroundColor

Expand Down Expand Up @@ -56,7 +60,7 @@ export function Head(): ReactElement {
name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover"
/>
<style>{`:root{--nextra-primary-hue:${lightHue}deg;--nextra-primary-saturation:${lightSaturation}%;--nextra-navbar-height:64px;--nextra-menu-height:3.75rem;--nextra-banner-height:2.5rem;--nextra-bg:${bgColor.light};}.dark{--nextra-primary-hue:${darkHue}deg;--nextra-primary-saturation:${darkSaturation}%;--nextra-bg:${bgColor.dark};}`}</style>
<style>{`:root{--nextra-primary-hue:${lightHue}deg;--nextra-primary-saturation:${lightSaturation}%;--nextra-primary-lightness:${lightLightness}%;--nextra-navbar-height:64px;--nextra-menu-height:3.75rem;--nextra-banner-height:2.5rem;--nextra-bg:${bgColor.light};}.dark{--nextra-primary-hue:${darkHue}deg;--nextra-primary-saturation:${darkSaturation}%;--nextra-primary-lightness:${darkLightness}%;--nextra-bg:${bgColor.dark};}`}</style>
{head}
</NextHead>
)
Expand Down
4 changes: 4 additions & 0 deletions packages/nextra-theme-docs/src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const DEFAULT_THEME: DocsThemeConfig = {
dark: 204,
light: 212
},
lightness: {
dark: 55,
light: 45
},
saturation: 100
},
darkMode: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/nextra-theme-docs/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export const themeSchema = /* @__PURE__ */ (() =>
dark: z.number(),
light: z.number()
})
),
lightness: z.number().or(
z.strictObject({
dark: z.number(),
light: z.number()
})
)
}),
project: z.strictObject({
Expand Down
24 changes: 12 additions & 12 deletions packages/nextra-theme-docs/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const makePrimaryColor: any =
(l: number) =>
({ opacityValue }: { opacityValue?: string }) => {
return (
`hsl(var(--nextra-primary-hue) var(--nextra-primary-saturation) ${l}%` +
`hsl(var(--nextra-primary-hue) var(--nextra-primary-saturation) calc(var(--nextra-primary-lightness) + ${l}%)` +
(opacityValue ? ` / ${opacityValue})` : ')')
)
}
Expand Down Expand Up @@ -52,17 +52,17 @@ export default {
blue: colors.blue,
yellow: colors.yellow,
primary: {
50: makePrimaryColor(97),
100: makePrimaryColor(94),
200: makePrimaryColor(86),
300: makePrimaryColor(77),
400: makePrimaryColor(66),
500: makePrimaryColor(50),
600: makePrimaryColor(45),
700: makePrimaryColor(39),
750: makePrimaryColor(35),
800: makePrimaryColor(32),
900: makePrimaryColor(24)
50: makePrimaryColor(52),
100: makePrimaryColor(49),
200: makePrimaryColor(41),
300: makePrimaryColor(32),
400: makePrimaryColor(21),
500: makePrimaryColor(5),
600: makePrimaryColor(0),
700: makePrimaryColor(-6),
750: makePrimaryColor(-10),
800: makePrimaryColor(-13),
900: makePrimaryColor(-11)
}
}
},
Expand Down

0 comments on commit d755012

Please sign in to comment.