Skip to content

Commit a6cf1d0

Browse files
committed
Add light theme CSS selector
1 parent 134fc2b commit a6cf1d0

File tree

9 files changed

+80
-30
lines changed

9 files changed

+80
-30
lines changed

.changeset/serious-foxes-sniff.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"bright": patch
3+
---
4+
5+
Add lightThemeSelector
6+
7+
```
8+
Code.theme = {
9+
dark: "min-dark",
10+
light: "min-light",
11+
lightSelector: ".light", // default is [data-theme="light"]
12+
}
13+
```

.gitpod.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
7+
tasks:
8+
- init: yarn install && yarn run build
9+
command: yarn run dev
10+
11+

lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"watch": "tsup src/index.tsx --format esm,cjs --watch --dts"
1717
},
1818
"dependencies": {
19-
"@code-hike/lighter": "0.6.2",
19+
"@code-hike/lighter": "0.6.6",
2020
"server-only": "^0.0.1"
2121
},
2222
"devDependencies": {

lib/src/code.tsx

+17-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export function Root(props: BrightProps) {
6969
...style,
7070
}}
7171
>
72-
<Style mode={mode} lineNumbers={props.lineNumbers} />
72+
<Style
73+
mode={mode}
74+
lineNumbers={props.lineNumbers}
75+
lightThemeSelector={props.lightThemeSelector}
76+
/>
7377
{title && <TitleBar {...props} />}
7478
<Pre {...props} />
7579
</div>
@@ -102,9 +106,11 @@ export function Pre(brightProps: BrightProps) {
102106
function Style({
103107
mode,
104108
lineNumbers,
109+
lightThemeSelector,
105110
}: {
106111
mode: "dark" | "light" | undefined
107112
lineNumbers?: boolean
113+
lightThemeSelector?: string
108114
}) {
109115
const lineNumbersStyle = `[data-bright-theme] [data-bright-ln] {
110116
color: var(--line-number-color);
@@ -114,21 +120,27 @@ function Style({
114120
user-select: none;
115121
}`
116122

117-
const css = `${displayStyle(mode)}
123+
const css = `${displayStyle(mode, lightThemeSelector)}
118124
[data-bright-theme] ::selection { background-color: var(--selection-background) }
119125
${lineNumbers ? lineNumbersStyle : ""}
120126
`
121127
return <style dangerouslySetInnerHTML={{ __html: css }} />
122128
}
123129

124-
function displayStyle(mode: "dark" | "light" | undefined) {
130+
function displayStyle(
131+
mode: "dark" | "light" | undefined,
132+
lightThemeSelector: string = '[data-theme="light"]'
133+
) {
125134
if (!mode) return ""
135+
126136
if (mode === "dark")
127137
return `[data-bright-mode="dark"] { display: block }
128-
[data-theme="light"] [data-bright-mode="dark"] { display: none }`
138+
${lightThemeSelector} [data-bright-mode="dark"] { display: none }`
139+
129140
if (mode === "light")
130141
return `[data-bright-mode="light"] { display: none }
131-
[data-theme="light"] [data-bright-mode="light"] { display: block }`
142+
${lightThemeSelector} [data-bright-mode="light"] { display: block }`
143+
132144
return ""
133145
}
134146

lib/src/index.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,19 @@ const Code: CodeComponent = async (componentProps) => {
4747
return (
4848
<>
4949
{/* @ts-expect-error Server Component */}
50-
<AnnotatedCode {...props} theme={darkTheme} mode="dark" />
50+
<AnnotatedCode
51+
{...props}
52+
theme={darkTheme}
53+
mode="dark"
54+
lightThemeSelector={doubleTheme.lightSelector}
55+
/>
5156
{/* @ts-expect-error Server Component */}
52-
<AnnotatedCode {...props} theme={lightTheme} mode="light" />
57+
<AnnotatedCode
58+
{...props}
59+
theme={lightTheme}
60+
mode="light"
61+
lightThemeSelector={doubleTheme.lightSelector}
62+
/>
5363
</>
5464
)
5565
}

lib/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type Extension = Prettify<
4545
export type Extensions = Extension[]
4646

4747
export type DoubleTheme = {
48+
lightSelector?: string
4849
dark: Theme
4950
light: Theme
5051
}
@@ -77,6 +78,7 @@ export type CodeProps = Prettify<
7778
lang: LanguageAlias
7879
code: string
7980
mode?: "dark" | "light"
81+
lightThemeSelector?: string
8082

8183
// atttributes
8284
style?: React.CSSProperties

web/app/icons.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function DiscordLink(props) {
5050
<a
5151
target="_blank"
5252
rel="noopener noreferrer"
53-
href="https://discord.gg/zYJMDBANkj"
53+
href="http://discord.codehike.org"
5454
{...props}
5555
>
5656
<svg

web/app/test/page.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import { Code, tokensToContent } from "bright"
2-
import { Wrapper, Input } from "../../client-components"
1+
import { Code } from "bright"
32

4-
Code.theme = "dracula"
5-
Code.extensions = [
6-
{
7-
name: "input",
8-
InlineAnnotation: ({ query, tokens }) => (
9-
<Input defaultValue={tokensToContent(tokens)} name={query} />
10-
),
11-
},
12-
]
3+
Code.theme = {
4+
dark: "min-dark",
5+
light: "min-light",
6+
lightSelector: ".light",
7+
}
8+
Code.extensions = []
139
const code = `
1410
<div
1511
style={{
16-
// input[13:19] color
1712
color: "#fafafa",
18-
// input[18:24] bg
1913
background: "#222222",
2014
}}
2115
>
2216
Hello World
23-
</div>`
17+
</div>`.trim()
2418

2519
export default function Page() {
2620
return (
27-
<Wrapper>
28-
<Code lang="jsx">{code}</Code>
29-
</Wrapper>
21+
<main>
22+
<div className="light">
23+
<Code lang="jsx">{code}</Code>
24+
</div>
25+
<div>
26+
<Code lang="jsx">{code}</Code>
27+
</div>
28+
<div data-theme="dark">
29+
<Code lang="jsx">{code}</Code>
30+
</div>
31+
</main>
3032
)
3133
}

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@
237237
human-id "^1.0.2"
238238
prettier "^2.7.1"
239239

240-
"@code-hike/[email protected].2":
241-
version "0.6.2"
242-
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.6.2.tgz#3f5cb79de86878ae0540ea36635591a7d8a82fbb"
243-
integrity sha512-yGzUE63b6dRIZFZ5Bn5XKRDsWQSNkGTvYSBZi0lXm2KSI3JKyM32DEns9Oe7aHQF/lf43+r5LjHoBY5K19+Sgw==
240+
"@code-hike/[email protected].6":
241+
version "0.6.6"
242+
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.6.6.tgz#72f1b93dceacc40022a0c2c0f154363e2595a4ba"
243+
integrity sha512-dLkSpowP4NaWDUgay3Mebb8V+wYqVGRD+Tl8wSfWdtIH0z4OUU3UznSsmvvGpCgkdUIF5DhOPRGRowqkNWQsrg==
244244

245245
"@esbuild/[email protected]":
246246
version "0.15.18"

0 commit comments

Comments
 (0)