Skip to content

Commit f073eba

Browse files
MatthematicMatthew Carr
andauthored
Added support for custom prism themes in source addon (#619)
* Added support for custom prism themes in source addon * added changeset * updated website docs for source addon --------- Co-authored-by: Matthew Carr <[email protected]>
1 parent 08856d9 commit f073eba

File tree

7 files changed

+94
-28
lines changed

7 files changed

+94
-28
lines changed

.changeset/swift-rivers-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ladle/react": minor
3+
---
4+
5+
Added support for custom prism themes in the source addon

packages/example/.ladle/config.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@
1111
*
1212
* */
1313

14+
const customLightTheme = {
15+
plain: {
16+
color: "black",
17+
backgroundColor: "hsl(180deg 75.96% 77.9%)",
18+
},
19+
};
20+
21+
const customDarkTheme = {
22+
plain: {
23+
color: "salmon",
24+
backgroundColor: "#1E1E1E",
25+
},
26+
};
27+
/** @type {import('@ladle/react').UserConfig} */
1428
export default {
1529
appendToHead: `<style>.append {}</style>`,
1630
addons: {
1731
a11y: {
1832
enabled: true,
1933
},
34+
source: {
35+
themeLight: customLightTheme,
36+
themeDark: customDarkTheme,
37+
},
2038
},
2139
expandStoryTree: true,
2240
};

packages/ladle/lib/app/src/addons/source.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import * as React from "react";
2-
import queryString from "query-string";
3-
import { Highlight, themes } from "prism-react-renderer";
41
import type { Language } from "prism-react-renderer";
2+
import { Highlight } from "prism-react-renderer";
3+
import queryString from "query-string";
4+
import * as React from "react";
55
import { useHotkeys } from "react-hotkeys-hook";
66
import {
7-
storySource,
87
stories,
8+
storySource,
99
StorySourceHeader,
1010
} from "virtual:generated-list";
11-
import { AddonProps, GlobalState, ActionType } from "../../../shared/types";
11+
import { ActionType, AddonProps, GlobalState } from "../../../shared/types";
12+
import config from "../get-config";
1213
import { Source } from "../icons";
1314
import { Modal } from "../ui";
14-
import config from "../get-config";
1515

1616
export const getQuery = (locationSearch: string) => {
1717
const urlVal = queryString.parse(locationSearch).source;
@@ -41,17 +41,16 @@ export const CodeHighlight = ({
4141

4242
if (match) {
4343
language = match[1] as Language;
44+
4445
return (
4546
<Highlight
4647
code={children.trim()}
4748
language={language}
48-
theme={{
49-
...(theme === "dark" ? themes.nightOwl : themes.github),
50-
plain: {
51-
...(theme === "dark" ? themes.nightOwl : themes.github).plain,
52-
backgroundColor: "var(--ladle-bg-color-secondary)",
53-
},
54-
}}
49+
theme={
50+
theme === "dark"
51+
? config.addons.source.themeDark
52+
: config.addons.source.themeLight
53+
}
5554
>
5655
{({ className, style, tokens, getTokenProps }) => (
5756
<div
@@ -81,13 +80,11 @@ export const CodeHighlight = ({
8180
<Highlight
8281
code={children.trim()}
8382
language={language}
84-
theme={{
85-
...(theme === "dark" ? themes.nightOwl : themes.github),
86-
plain: {
87-
...(theme === "dark" ? themes.nightOwl : themes.github).plain,
88-
backgroundColor: "var(--ladle-bg-color-secondary)",
89-
},
90-
}}
83+
theme={
84+
theme === "dark"
85+
? config.addons.source.themeDark
86+
: config.addons.source.themeLight
87+
}
9188
>
9289
{({ className, style, tokens, getLineProps, getTokenProps }) => (
9390
<pre

packages/ladle/lib/shared/default-config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { themes } from "prism-react-renderer";
2+
13
/**
24
* @type {import('../shared/types').Config}
35
*/
@@ -59,6 +61,20 @@ export default {
5961
source: {
6062
enabled: true,
6163
defaultState: false,
64+
themeDark: {
65+
...themes.nightOwl,
66+
plain: {
67+
...themes.nightOwl.plain,
68+
backgroundColor: "var(--ladle-bg-color-secondary)",
69+
},
70+
},
71+
themeLight: {
72+
...themes.github,
73+
plain: {
74+
...themes.github.plain,
75+
backgroundColor: "var(--ladle-bg-color-secondary)",
76+
},
77+
},
6278
},
6379
a11y: {
6480
enabled: false,

packages/ladle/lib/shared/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PrismTheme } from "prism-react-renderer";
12
import type { UserConfig as ViteUserConfig } from "vite";
23

34
type RecursivePartial<T> = {
@@ -231,6 +232,8 @@ export type Config = {
231232
source: {
232233
enabled: boolean;
233234
defaultState: boolean;
235+
themeLight: PrismTheme;
236+
themeDark: PrismTheme;
234237
};
235238
a11y: {
236239
enabled: boolean;

0 commit comments

Comments
 (0)