Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: insert site custom CSS with highest priority #6227

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ function testValidateThemeConfig(partialThemeConfig) {
});
}

function testOk(partialThemeConfig) {
expect(
testValidateThemeConfig({...DEFAULT_CONFIG, ...partialThemeConfig}),
).toEqual({
...DEFAULT_CONFIG,
...partialThemeConfig,
});
}

describe('themeConfig', () => {
test('should accept valid theme config', () => {
const userConfig = {
Expand Down Expand Up @@ -477,31 +468,13 @@ describe('themeConfig', () => {
});

describe('customCss config', () => {
test('should accept customCss undefined', () => {
testOk({
customCss: undefined,
});
});

test('should accept customCss string', () => {
testOk({
customCss: './path/to/cssFile.css',
});
});

test('should accept customCss string array', () => {
testOk({
customCss: ['./path/to/cssFile.css', './path/to/cssFile2.css'],
});
});

test('should reject customCss number', () => {
test('should reject customCss string', () => {
expect(() =>
testValidateThemeConfig({
customCss: 42,
customCss: './path/to/cssFile.css',
}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"customCss\\" must be one of [array, string]"`,
`"themeConfig.customCss is invalid. Custom css used to be provided as themeOptions.customCss, but it is deprecated now."`,
);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-classic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,4 @@ export function getSwizzleComponentList(): string[] {
}

export {validateThemeConfig} from './validateThemeConfig';
export {validateOptions} from './options';
49 changes: 49 additions & 0 deletions packages/docusaurus-theme-classic/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {Joi} from '@docusaurus/utils-validation';
import type {
ValidationResult,
OptionValidationContext,
} from '@docusaurus/types';
import type {Options} from '@docusaurus/theme-classic';

const DEFAULT_OPTIONS: Options = {
customCss: null,
};

export const Schema = Joi.object({
customCss: Joi.alternatives()
.try(Joi.array().items(Joi.string().required()), Joi.string().required())
.optional()
.default(DEFAULT_OPTIONS.customCss)
.warning('deprecate.error', {
msg: `theme.customCss option is deprecated.
Please use siteConfig.style.css instead.

Note that this also changes the CSS insertion order!
From

Before: custom site CSS was inserted before Infima CSS and theme modules.
After: custom site CSS will be inserted after Infima CSS and theme modules.

This enables your site CSS to override default theme CSS more easily, without using !important

See also https://github.com/facebook/docusaurus/pull/6227
slorber marked this conversation as resolved.
Show resolved Hide resolved
`,
})
.messages({
'deprecate.error': '{#msg}',
}),
});

export function validateOptions({
validate,
options,
}: OptionValidationContext<Options>): ValidationResult<Options> {
return validate(Schema, options);
}
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

declare module '@docusaurus/theme-classic' {
export type Options = {
customCss?: string | string[];
customCss?: string | string[] | null;
};
}

Expand Down
12 changes: 7 additions & 5 deletions packages/docusaurus-theme-classic/src/validateThemeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ const FooterLinkItemSchema = Joi.object({
// (users may need additional attributes like target, aria-role, data-customAttribute...)
.unknown();

const CustomCssSchema = Joi.alternatives()
.try(Joi.array().items(Joi.string().required()), Joi.string().required())
.optional();

const ThemeConfigSchema = Joi.object({
// TODO temporary (@alpha-58)
disableDarkMode: Joi.any().forbidden().messages({
Expand All @@ -259,7 +255,13 @@ const ThemeConfigSchema = Joi.object({
'any.unknown':
'defaultDarkMode theme config is deprecated. Please use the new colorMode attribute. You likely want: config.themeConfig.colorMode.defaultMode = "dark"',
}),
customCss: CustomCssSchema,

// TODO temporary
customCss: Joi.any().forbidden().messages({
slorber marked this conversation as resolved.
Show resolved Hide resolved
'any.unknown':
'themeConfig.customCss is invalid. Custom css used to be provided as themeOptions.customCss, but it is deprecated now.',
}),

colorMode: ColorModeSchema,
image: Joi.string(),
docs: DocsSchema,
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ interface HtmlTagObject {
innerHTML?: string;
}

// TODO weird useless type, refactor
export type ValidationResult<T> = T;
slorber marked this conversation as resolved.
Show resolved Hide resolved

export type ValidationSchema<T> = Joi.ObjectSchema<T>;
Expand Down
2 changes: 1 addition & 1 deletion website/testCSSOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const EXPECTED_CSS_MARKERS = [
'.tabs__item',

// Test markers
'.test-marker-site-custom-css-unique-rule',
'.test-marker-site-client-module',
'.test-marker-theme-layout',
'.test-marker-site-index-page',
'.test-marker-site-custom-css-unique-rule',

// lazy loaded lib
'.DocSearch-Modal',
Expand Down