Skip to content

Commit b99d276

Browse files
committed
Remove theme from annotations
1 parent c5acd6f commit b99d276

File tree

5 files changed

+500
-102
lines changed

5 files changed

+500
-102
lines changed

packages/mdx/src/index.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@
6161
border-radius: 0.25rem;
6262
padding: 0.2rem 0.15rem 0.1rem;
6363
margin: 0 -0.15rem;
64+
background: var(--ch-l-editor-rangeHighlightBackground);
65+
}
66+
67+
.ch-code-multiline-mark {
68+
background: var(--ch-l-editor-rangeHighlightBackground);
6469
}
6570

6671
.ch-code-multiline-mark-border {
6772
width: 3px;
6873
height: 100%;
6974
position: absolute;
7075
left: 0;
76+
background: var(--ch-l-editor-infoForeground);
7177
}
7278

7379
.ch-code-multiline-mark .ch-code-button {
@@ -93,3 +99,7 @@
9399
text-decoration-style: dotted;
94100
color: inherit;
95101
}
102+
103+
.ch-code-box-annotation {
104+
outline: 2px solid var(--ch-l-editor-infoForeground);
105+
}

packages/mdx/src/mdx-client/annotations.tsx

Lines changed: 41 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React from "react"
22
import { CodeAnnotation } from "../smooth-code"
3-
import { getColor, transparent, ColorName } from "../utils"
4-
import { LineWithElement } from "../smooth-code/partial-step-parser"
3+
import { transparent } from "../utils"
4+
import {
5+
AnnotationProps,
6+
LineWithElement,
7+
} from "../smooth-code/partial-step-parser"
58
import { CopyButton } from "../smooth-code/copy-button"
9+
import { CSSV } from "utils/light/css"
610

711
export function Annotation() {
812
return (
@@ -36,35 +40,19 @@ function MultilineMark({
3640
children,
3741
data,
3842
style,
39-
theme,
4043
lines,
4144
}: {
4245
data: string
4346
children: React.ReactNode
4447
style?: React.CSSProperties
45-
theme?: any
4648
lines?: LineWithElement[]
4749
}) {
4850
const content = getContent(lines)
4951
const className = `ch-code-multiline-mark ` + (data ?? "")
50-
const bg = getColor(
51-
theme,
52-
ColorName.RangeHighlightBackground
53-
)
54-
const border = getColor(
55-
theme,
56-
ColorName.EditorInfoForeground
57-
)
5852

5953
return (
60-
<div
61-
style={{ ...style, background: bg }}
62-
className={className}
63-
>
64-
<span
65-
className="ch-code-multiline-mark-border"
66-
style={{ background: border }}
67-
/>
54+
<div style={style} className={className}>
55+
<span className="ch-code-multiline-mark-border" />
6856
{children}
6957
<CopyButton
7058
className="ch-code-button"
@@ -88,25 +76,15 @@ function getContent(lines: LineWithElement[]) {
8876
.join("\n")
8977
}
9078

91-
function InlineMark({
92-
children,
93-
data,
94-
theme,
95-
}: {
96-
data: any
97-
children: React.ReactNode
98-
theme: any
99-
}) {
100-
const bg =
101-
tryGuessColor(children) ||
102-
transparent(
103-
getColor(theme, ColorName.CodeForeground),
104-
0.2
105-
)
106-
79+
function InlineMark({ children, data }: AnnotationProps) {
10780
const className = "ch-code-inline-mark " + (data ?? "")
10881
return (
109-
<span className={className} style={{ background: bg }}>
82+
<span
83+
className={className}
84+
style={{
85+
background: tryGuessColor(children) || undefined,
86+
}}
87+
>
11088
{children}
11189
</span>
11290
)
@@ -115,44 +93,34 @@ function InlineMark({
11593
function tryGuessColor(
11694
children: React.ReactNode
11795
): string | undefined {
118-
const child = React.Children.toArray(children)[0] as any
119-
120-
const grandChild = React.Children.toArray(
121-
child?.props?.children || []
122-
)[0] as any
123-
124-
const grandGrandChild = React.Children.toArray(
125-
grandChild?.props?.children || []
126-
)[0] as any
127-
128-
const { color } = grandGrandChild?.props?.style || {}
129-
130-
if (color) {
131-
return transparent(color as string, 0.2)
96+
try {
97+
const child = React.Children.toArray(children)[0] as any
98+
99+
const grandChild = React.Children.toArray(
100+
child?.props?.children || []
101+
)[0] as any
102+
103+
const grandGrandChild = React.Children.toArray(
104+
grandChild?.props?.children || []
105+
)[0] as any
106+
const { color } = grandGrandChild?.props?.style || {}
107+
108+
if (color) {
109+
return transparent(color as string, 0.2)
110+
}
111+
return undefined
112+
} catch (e) {
113+
return undefined
132114
}
133-
134-
return undefined
135115
}
136116

137-
function Box({
138-
children,
139-
data,
140-
theme,
141-
}: {
142-
data: any
143-
children: React.ReactNode
144-
theme: any
145-
}) {
146-
const border =
147-
typeof data === "string"
148-
? data
149-
: theme.tokenColors.find((tc: any) =>
150-
tc.scope?.includes("string")
151-
)?.settings?.foreground || "yellow"
117+
function Box({ children, data }: AnnotationProps) {
118+
const outlineColor =
119+
typeof data === "string" ? data : undefined
152120
return (
153121
<span
154122
className="ch-code-box-annotation"
155-
style={{ outline: `2px solid ${border}` }}
123+
style={{ outlineColor }}
156124
>
157125
{children}
158126
</span>
@@ -163,38 +131,16 @@ function WithClass({
163131
children,
164132
data,
165133
style,
166-
theme,
167-
}: {
168-
data: any
169-
children: React.ReactNode
170-
style?: React.CSSProperties
171-
theme: any
172-
}) {
173-
const className = data as string
134+
}: AnnotationProps) {
174135
return (
175-
<span style={style} className={className}>
136+
<span style={style} className={data as string}>
176137
{children}
177138
</span>
178139
)
179140
}
180141

181-
function Label({
182-
children,
183-
data,
184-
style,
185-
theme,
186-
}: {
187-
data: any
188-
children: React.ReactNode
189-
style?: React.CSSProperties
190-
theme?: any
191-
}) {
192-
const bg = ((theme as any).colors[
193-
"editor.lineHighlightBackground"
194-
] ||
195-
(theme as any).colors[
196-
"editor.selectionHighlightBackground"
197-
]) as string
142+
function Label({ children, data, style }: AnnotationProps) {
143+
const bg = CSSV.editor.lineHighlightBackground
198144
const [hover, setHover] = React.useState(false)
199145

200146
return (

packages/mdx/src/smooth-code/partial-step-parser.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ import {
1717
annotateMultiline,
1818
} from "./annotations"
1919

20+
export type AnnotationProps = {
21+
style?: React.CSSProperties
22+
children: React.ReactNode
23+
data: any
24+
isInline: boolean
25+
}
26+
2027
export type CodeAnnotation = {
2128
focus: string
22-
Component?: (props: {
23-
style?: React.CSSProperties
24-
children: React.ReactNode
25-
data: any
26-
theme: EditorTheme
27-
isInline: boolean
28-
}) => React.ReactElement
29+
Component?: (props: AnnotationProps) => React.ReactElement
2930
data?: any
3031
// sometimes serializing the Component function doesn't work (Astro)
3132
// so we pass the name and get the Component from annotationsMap
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { Theme } from "@code-hike/lighter"
2+
import { getColor, getTheme } from "./theme-colors"
3+
4+
const keys = [
5+
"editor.background",
6+
"editor.foreground",
7+
"editor.lineHighlightBackground",
8+
"editor.rangeHighlightBackground",
9+
"editor.infoForeground",
10+
"editor.selectionBackground",
11+
"focusBorder",
12+
"tab.activeBackground",
13+
"tab.activeForeground",
14+
"tab.inactiveBackground",
15+
"tab.inactiveForeground",
16+
"tab.border",
17+
"tab.activeBorder",
18+
"editorGroup.border",
19+
"editorGroupHeader.tabsBackground",
20+
"editorLineNumber.foreground",
21+
"input.background",
22+
"input.foreground",
23+
"input.border",
24+
"icon.foreground",
25+
"sideBar.background",
26+
"sideBar.foreground",
27+
"sideBar.border",
28+
"list.activeSelectionBackground",
29+
"list.activeSelectionForeground",
30+
"list.hoverBackground",
31+
"list.hoverForeground",
32+
]
33+
34+
function v(key: string) {
35+
return `var(${vn(key)})`
36+
}
37+
38+
function vn(key: string) {
39+
return `--ch-l-${key.replace(".", "-")}`
40+
}
41+
42+
export const CSSV = {
43+
editor: {
44+
background: v("editor.background"),
45+
foreground: v("editor.foreground"),
46+
lineHighlightBackground: v(
47+
"editor.lineHighlightBackground"
48+
),
49+
rangeHighlightBackground: v(
50+
"editor.rangeHighlightBackground"
51+
),
52+
infoForeground: v("editor.infoForeground"),
53+
selectionBackground: v("editor.selectionBackground"),
54+
},
55+
focusBorder: v("focusBorder"),
56+
tab: {
57+
activeBackground: v("tab.activeBackground"),
58+
activeForeground: v("tab.activeForeground"),
59+
inactiveBackground: v("tab.inactiveBackground"),
60+
inactiveForeground: v("tab.inactiveForeground"),
61+
border: v("tab.border"),
62+
activeBorder: v("tab.activeBorder"),
63+
},
64+
editorGroup: {
65+
border: v("editorGroup.border"),
66+
},
67+
editorGroupHeader: {
68+
tabsBackground: v("editorGroupHeader.tabsBackground"),
69+
},
70+
editorLineNumber: {
71+
foreground: v("editorLineNumber.foreground"),
72+
},
73+
input: {
74+
background: v("input.background"),
75+
foreground: v("input.foreground"),
76+
border: v("input.border"),
77+
},
78+
icon: {
79+
foreground: v("icon.foreground"),
80+
},
81+
sideBar: {
82+
background: v("sideBar.background"),
83+
foreground: v("sideBar.foreground"),
84+
border: v("sideBar.border"),
85+
},
86+
list: {
87+
activeSelectionBackground: v(
88+
"list.activeSelectionBackground"
89+
),
90+
activeSelectionForeground: v(
91+
"list.activeSelectionForeground"
92+
),
93+
hoverBackground: v("list.hoverBackground"),
94+
hoverForeground: v("list.hoverForeground"),
95+
},
96+
}
97+
98+
export function getCSSVariables(theme: Theme) {
99+
const finalTheme = getTheme(theme)
100+
const vars = {}
101+
for (const key of keys) {
102+
vars[vn(key)] = getColor(finalTheme, key)
103+
}
104+
return vars
105+
}

0 commit comments

Comments
 (0)