Skip to content

Commit 679e633

Browse files
authored
Merge pull request #20 from midudev/midudev-add-light-theme-support
Add light theme support
2 parents 9e42895 + 52c6b14 commit 679e633

10 files changed

Lines changed: 649 additions & 151 deletions

File tree

src/components/ArrayVisualizer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default function ArrayVisualizer({ step }: ArrayVisualizerProps) {
7777
? highlightColors[highlight]
7878
: isSorted
7979
? highlightColors.sorted
80-
: '#333'
80+
: 'var(--viz-faint)'
8181

8282
return (
8383
<div

src/components/CodePanel.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ interface CodePanelProps {
4141
locale?: Locale
4242
}
4343

44-
/** Custom dark theme that matches the app background */
45-
function defineTheme(monaco: Monaco) {
44+
function defineThemes(monaco: Monaco) {
4645
monaco.editor.defineTheme('algoviz-dark', {
4746
base: 'vs-dark',
4847
inherit: true,
@@ -56,6 +55,19 @@ function defineTheme(monaco: Monaco) {
5655
'editorCursor.foreground': '#fff',
5756
},
5857
})
58+
monaco.editor.defineTheme('algoviz-light', {
59+
base: 'vs',
60+
inherit: true,
61+
rules: [],
62+
colors: {
63+
'editor.background': '#ffffff',
64+
'editor.lineHighlightBackground': '#00000006',
65+
'editorLineNumber.foreground': '#b3b3b3',
66+
'editorLineNumber.activeForeground': '#ca8a04',
67+
'editor.selectionBackground': '#00000015',
68+
'editorCursor.foreground': '#171717',
69+
},
70+
})
5971
}
6072

6173
export default function CodePanel({
@@ -80,10 +92,22 @@ export default function CodePanel({
8092
setIsMounted(true)
8193
}, [])
8294

95+
useEffect(() => {
96+
const updateEditorTheme = (event?: Event) => {
97+
const theme =
98+
event instanceof CustomEvent ? event.detail : document.documentElement.dataset.theme
99+
monacoRef.current?.editor.setTheme(theme === 'light' ? 'algoviz-light' : 'algoviz-dark')
100+
}
101+
window.addEventListener('themechange', updateEditorTheme)
102+
return () => window.removeEventListener('themechange', updateEditorTheme)
103+
}, [])
104+
83105
const handleEditorDidMount = useCallback(
84106
(editor: any, monacoInstance: Monaco) => {
85-
defineTheme(monacoInstance)
86-
monacoInstance.editor.setTheme('algoviz-dark')
107+
defineThemes(monacoInstance)
108+
monacoInstance.editor.setTheme(
109+
document.documentElement.dataset.theme === 'light' ? 'algoviz-light' : 'algoviz-dark',
110+
)
87111
editorRef.current = editor
88112
monacoRef.current = monacoInstance
89113

@@ -232,7 +256,11 @@ export default function CodePanel({
232256
<LazyEditor
233257
defaultLanguage="javascript"
234258
value={code}
235-
theme="vs-dark"
259+
theme={
260+
document.documentElement.dataset.theme === 'light'
261+
? 'algoviz-light'
262+
: 'algoviz-dark'
263+
}
236264
onMount={handleEditorDidMount}
237265
loading={null}
238266
options={{

src/components/ComplexityChart.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ function parseTimeComplexity(description: string): Entry[] {
7575
if (!blockMatch) return entries
7676
const block = blockMatch[0]
7777

78-
const oRe = (prefix: string) =>
79-
new RegExp(`${prefix}:\\s*(O\\((?:[^()]+|\\([^)]*\\))*\\))`, 'i')
78+
const oRe = (prefix: string) => new RegExp(`${prefix}:\\s*(O\\((?:[^()]+|\\([^)]*\\))*\\))`, 'i')
8079

8180
const best = block.match(oRe(isSpanish ? 'Mejor' : 'Best'))
8281
const avg = block.match(oRe(isSpanish ? 'Promedio' : 'Average'))
@@ -85,15 +84,28 @@ function parseTimeComplexity(description: string): Entry[] {
8584
if (best || avg || worst) {
8685
if (best) {
8786
const k = normalizeToKey(best[1])
88-
if (k) entries.push({ label: isSpanish ? 'Mejor' : 'Best', raw: best[1], key: k, color: '#34d399' })
87+
if (k)
88+
entries.push({
89+
label: isSpanish ? 'Mejor' : 'Best',
90+
raw: best[1],
91+
key: k,
92+
color: '#34d399',
93+
})
8994
}
9095
if (avg) {
9196
const k = normalizeToKey(avg[1])
92-
if (k) entries.push({ label: isSpanish ? 'Prom' : 'Avg', raw: avg[1], key: k, color: '#fbbf24' })
97+
if (k)
98+
entries.push({ label: isSpanish ? 'Prom' : 'Avg', raw: avg[1], key: k, color: '#fbbf24' })
9399
}
94100
if (worst) {
95101
const k = normalizeToKey(worst[1])
96-
if (k) entries.push({ label: isSpanish ? 'Peor' : 'Worst', raw: worst[1], key: k, color: '#f87171' })
102+
if (k)
103+
entries.push({
104+
label: isSpanish ? 'Peor' : 'Worst',
105+
raw: worst[1],
106+
key: k,
107+
color: '#f87171',
108+
})
97109
}
98110
} else {
99111
const single = block.match(O_RE)
@@ -162,7 +174,13 @@ function resolveOverlaps(
162174
return sorted
163175
}
164176

165-
export default function ComplexityChart({ description, locale = 'en' }: { description: string; locale?: string }) {
177+
export default function ComplexityChart({
178+
description,
179+
locale = 'en',
180+
}: {
181+
description: string
182+
locale?: string
183+
}) {
166184
const entries = useMemo(() => parseTimeComplexity(description), [description])
167185

168186
if (entries.length === 0) return null
@@ -179,7 +197,12 @@ export default function ComplexityChart({ description, locale = 'en' }: { descri
179197
const labels: { y: number; text: string; color: string; opacity: number }[] = []
180198
for (const key of REFERENCE_KEYS) {
181199
if (!highlightedKeys.has(key)) {
182-
labels.push({ y: endY(COMPLEXITY_FNS[key]), text: key, color: '#fff', opacity: 0.18 })
200+
labels.push({
201+
y: endY(COMPLEXITY_FNS[key]),
202+
text: key,
203+
color: 'var(--foreground)',
204+
opacity: 0.18,
205+
})
183206
}
184207
}
185208
for (const [key, group] of grouped) {
@@ -300,10 +323,7 @@ export default function ComplexityChart({ description, locale = 'en' }: { descri
300323
<div className="flex flex-wrap gap-x-4 gap-y-1 mt-2">
301324
{entries.map((e, i) => (
302325
<div key={i} className="flex items-center gap-1.5">
303-
<div
304-
className="w-3 h-0.5 rounded-full shrink-0"
305-
style={{ backgroundColor: e.color }}
306-
/>
326+
<div className="w-3 h-0.5 rounded-full shrink-0" style={{ backgroundColor: e.color }} />
307327
<span className="text-[10px] text-neutral-500">
308328
{e.label ? `${e.label}: ` : ''}
309329
<span className="font-mono text-neutral-400">{e.raw}</span>

0 commit comments

Comments
 (0)