Skip to content

Commit ccaf3d4

Browse files
87xiedimaMachina
andauthoredSep 11, 2024··
[v3] add auto-import-theme-style option in nextra configuration (#3220)
* [v3] add auto-import-theme-style option in nextra configuration * remove the style field in `package.json` * Update .changeset/red-rockets-perform.md Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com> * dry in styles.css swr-site example --------- Co-authored-by: Dimitri POSTOLOV <dmytropostolov@gmail.com>
1 parent c3ff165 commit ccaf3d4

File tree

10 files changed

+50
-46
lines changed

10 files changed

+50
-46
lines changed
 

‎.changeset/red-rockets-perform.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'nextra-theme-docs': patch
3+
'nextra': patch
4+
---
5+
6+
Add the `autoImportThemeStyle` option to the Nextra configuration. This allows users to import the official Nextra theme CSS into a specific cascade layer.

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ out/
1313

1414
tsup.config.bundled*
1515
tsconfig.tsbuildinfo
16+
pagefind/

‎examples/swr-site/next.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function getStaticProps() {
3232
}
3333
return pageMap
3434
},
35-
latex: true
35+
latex: true,
36+
autoImportThemeStyle: false
3637
})
3738

3839
const withBundleAnalyzer = bundleAnalyzer({

‎examples/swr-site/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@next/bundle-analyzer": "^13.4.19",
3737
"autoprefixer": "^10.4.16",
3838
"postcss": "^8.4.33",
39+
"postcss-import": "^16.0.0",
3940
"tailwindcss": "^3.4.1"
4041
},
4142
"nextBundleAnalysis": {

‎examples/swr-site/postcss.config.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @type {import('postcss').Postcss} */
22
module.exports = {
33
plugins: {
4+
'postcss-import': {},
45
tailwindcss: {},
56
autoprefixer: {}
67
}

‎examples/swr-site/styles.css

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1+
@import 'nextra-theme-docs/dist/style.css' layer(nextra-base);
2+
13
@tailwind utilities;
24

35
.dark .invert-on-dark {
46
filter: invert(1) brightness(1.8);
57
}
68

79
body {
8-
background:
9-
linear-gradient(
10-
to bottom,
11-
rgba(255, 255, 255, 0) 0%,
12-
rgb(var(--nextra-bg)) 300px
13-
),
14-
fixed 0 0 / 20px 20px radial-gradient(#d1d1d1 1px, transparent 0),
15-
fixed 10px 10px / 20px 20px radial-gradient(#d1d1d1 1px, transparent 0);
16-
}
10+
--bg-dot-color: #d1d1d1;
11+
12+
.dark & {
13+
--bg-dot-color: #313131;
14+
}
1715

18-
.dark body {
1916
background:
20-
linear-gradient(
21-
to bottom,
22-
rgba(0, 0, 0, 0) 0%,
23-
rgb(var(--nextra-bg)) 300px
24-
),
25-
fixed 0 0 / 20px 20px radial-gradient(#313131 1px, transparent 0),
26-
fixed 10px 10px / 20px 20px radial-gradient(#313131 1px, transparent 0);
17+
linear-gradient(to bottom, transparent, rgb(var(--nextra-bg)) 300px),
18+
fixed 0 0 / 20px 20px
19+
radial-gradient(var(--bg-dot-color) 1px, transparent 0),
20+
fixed 10px 10px / 20px 20px
21+
radial-gradient(var(--bg-dot-color) 1px, transparent 0);
2722
}

‎packages/nextra/src/server/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const DEFAULT_CONFIG = {
2727
search: {
2828
codeblocks: true
2929
},
30-
codeHighlight: true
30+
codeHighlight: true,
31+
autoImportThemeStyle: true
3132
} satisfies Partial<NextraConfig>
3233

3334
export const OFFICIAL_THEMES = ['nextra-theme-docs', 'nextra-theme-blog']

‎packages/nextra/src/server/loader.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export async function loader(
6464
codeHighlight,
6565
transform,
6666
mdxOptions,
67-
locales
67+
locales,
68+
autoImportThemeStyle
6869
} = this.getOptions()
6970

7071
const mdxPath = this._module?.resourceResolveData
@@ -106,7 +107,7 @@ export const getStaticProps = () => ({ notFound: true })`
106107

107108
const cssImports = `
108109
${latex ? "import 'katex/dist/katex.min.css'" : ''}
109-
${OFFICIAL_THEMES.includes(theme) ? `import '${theme}/style.css'` : ''}`
110+
${OFFICIAL_THEMES.includes(theme) && autoImportThemeStyle ? `import '${theme}/style.css'` : ''}`
110111

111112
if (currentPath.includes('/pages/_app.')) {
112113
isAppFileFromNodeModules = currentPath.includes('/node_modules/')

‎packages/nextra/src/server/schemas.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export const nextraConfigSchema = z
106106
recmaPlugins: z.custom<ProcessorOptions['recmaPlugins']>(),
107107
format: z.enum(['detect', 'mdx', 'md']),
108108
rehypePrettyCodeOptions: z.custom<RehypePrettyCodeOptions>()
109-
})
109+
}),
110+
autoImportThemeStyle: z.boolean()
110111
})
111112
// eslint-disable-next-line deprecation/deprecation -- fixme
112113
.deepPartial()

‎pnpm-lock.yaml

+20-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.