Skip to content

Commit

Permalink
refactor: control base styling of code blocks via CSS vars
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed Apr 14, 2022
1 parent 6306cbc commit 5dfb338
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

.copyButton {
display: flex;
background: inherit;
/* TODO: move to base button styling */
background: var(--prism-background-color);
color: var(--prism-color);
border: 1px solid var(--ifm-color-emphasis-300);
border-radius: var(--ifm-global-radius);
padding: 0.4rem;
Expand Down
13 changes: 5 additions & 8 deletions packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function CodeBlock({
theme={prismTheme}
code=""
language={'text' as Language}>
{({className, style}) => (
{({className}) => (
<pre
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
tabIndex={0}
Expand All @@ -72,8 +72,7 @@ export default function CodeBlock({
styles.codeBlockContainer,
blockClassName,
ThemeClassNames.common.codeBlock,
)}
style={style}>
)}>
<code className={styles.codeBlockLines}>{children}</code>
</pre>
)}
Expand All @@ -99,7 +98,7 @@ export default function CodeBlock({
theme={prismTheme}
code={code}
language={(language ?? 'text') as Language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
{({className, tokens, getLineProps, getTokenProps}) => (
<div
className={clsx(
styles.codeBlockContainer,
Expand All @@ -111,11 +110,9 @@ export default function CodeBlock({
ThemeClassNames.common.codeBlock,
)}>
{codeBlockTitle && (
<div style={style} className={styles.codeBlockTitle}>
{codeBlockTitle}
</div>
<div className={styles.codeBlockTitle}>{codeBlockTitle}</div>
)}
<div className={styles.codeBlockContent} style={style}>
<div className={styles.codeBlockContent}>
<pre
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
tabIndex={0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

.codeBlockContainer {
background: var(--prism-background-color);
color: var(--prism-color);
margin-bottom: var(--ifm-leading);
box-shadow: var(--ifm-global-shadow-lw);
border-radius: var(--ifm-code-border-radius);
Expand All @@ -28,7 +30,7 @@
}

.codeBlock {
--ifm-pre-background: inherit;
--ifm-pre-background: var(--prism-background-color);
margin: 0;
padding: 0;
}
Expand All @@ -44,7 +46,6 @@

.codeBlockLines {
font: inherit;
background: var(--ifm-pre-background);
/* rtl:ignore */
float: left;
min-width: 100%;
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-theme-classic/src/validateThemeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import defaultPrismTheme from 'prism-react-renderer/themes/palenight';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {
ThemeConfig,
Expand Down Expand Up @@ -32,6 +33,7 @@ export const DEFAULT_CONFIG = {
metadata: [],
prism: {
additionalLanguages: [],
theme: defaultPrismTheme,
},
navbar: {
hideOnScroll: false,
Expand Down Expand Up @@ -335,7 +337,7 @@ export const ThemeConfigSchema = Joi.object({
theme: Joi.object({
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
}),
}).default(DEFAULT_CONFIG.prism.theme),
darkTheme: Joi.object({
plain: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
styles: Joi.alternatives().try(Joi.array(), Joi.object()).required(),
Expand Down
19 changes: 18 additions & 1 deletion packages/docusaurus-theme-common/src/contexts/colorMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const storeColorMode = (newColorMode: ColorMode) => {

function useContextValue(): ContextValue {
const {
prism,
colorMode: {defaultMode, disableSwitch, respectPrefersColorScheme},
} = useThemeConfig();
const [colorMode, setColorModeState] = useState(
Expand All @@ -75,7 +76,23 @@ function useContextValue(): ContextValue {
'data-theme',
coerceToColorMode(colorMode),
);
}, [colorMode]);

// Add Prism's CSS variables
const root = window.document.documentElement;
const currentPrismTheme =
(colorMode === 'dark' ? prism.darkTheme : prism.theme) || prism.theme;

Object.entries(currentPrismTheme.plain).forEach(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
([name, color]: [string, any]) => {
const cssVarName = `--prism-${name.replace(
/[a-z][A-Z\d]/g,
(m) => `${m[0]}-${m[1]!.toLowerCase()}`,
)}`;
root.style.setProperty(cssVarName, color);
},
);
}, [colorMode, prism.theme, prism.darkTheme]);

useEffect(() => {
if (disableSwitch) {
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-theme-common/src/hooks/usePrismTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import defaultTheme from 'prism-react-renderer/themes/palenight';
import type {PrismTheme} from 'prism-react-renderer';
import {useColorMode} from '../contexts/colorMode';
import {useThemeConfig} from '../utils/useThemeConfig';

/**
* Returns a color-mode-dependent Prism theme: whatever the user specified in
* the config. Falls back to `palenight`.
*/
export function usePrismTheme(): typeof defaultTheme {
export function usePrismTheme(): PrismTheme {
const {prism} = useThemeConfig();
const {colorMode} = useColorMode();
const lightModeTheme = prism.theme || defaultTheme;
const lightModeTheme = prism.theme;
const darkModeTheme = prism.darkTheme || lightModeTheme;
const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type AnnouncementBarConfig = {
};

export type PrismConfig = {
theme?: PrismTheme;
theme: PrismTheme;
darkTheme?: PrismTheme;
defaultLanguage?: string;
additionalLanguages: string[];
Expand Down

0 comments on commit 5dfb338

Please sign in to comment.