Skip to content

Commit 05c0d7e

Browse files
authored
feat: support custom global style (#92)
* feat: support custom global style * chore: optimize runtime code import * chore: add global style doc
1 parent 50e13ac commit 05c0d7e

File tree

13 files changed

+105
-15
lines changed

13 files changed

+105
-15
lines changed

docs/.island/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ function getSidebar(lang: 'zh' | 'en') {
126126
{
127127
text: getText('静态资源', 'Static Assets'),
128128
link: getLink('/guide/static-assets')
129+
},
130+
{
131+
text: getText('添加全局样式', 'Add Global Styles'),
132+
link: getLink('/guide/custom-global-style')
129133
}
130134
]
131135
},

docs/.island/styles/index.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Test for custom global style */
2+
/* :root {
3+
--island-c-brand: #5c6ac4;
4+
--island-c-brand-light: #7c87cf;
5+
--island-c-brand-dark: #4b5abf;
6+
--island-c-brand-darker: #3a49ba;
7+
} */

docs/en/guide/custom-global-style.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add Global Styles
2+
3+
In some cases, you may need to add some global styles based on the theme UI. Island.js provides a convention directory that allows you to add global styles without modifying the theme.
4+
5+
## Usage
6+
7+
You can create a `styles` folder in the `.island` directory, for example:
8+
9+
```bash
10+
.island
11+
├── config.ts
12+
└── styles
13+
└── index.css
14+
```
15+
16+
Then add the following code:
17+
18+
```css
19+
/* styles/index.css */
20+
:root {
21+
--island-c-brand: #f00;
22+
}
23+
```
24+
25+
Island.js will automatically collect the global styles you defined in `styles/index.css`.

docs/zh/guide/custom-global-style.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 自定义全局样式
2+
3+
某些场景下,你可能需要在主题 UI 的基础上添加一些全局样式,Island.js 提供了一个约定式目录让你能够在不修改主题的情况下添加全局样式。
4+
5+
## 使用方法
6+
7+
你可以在 `.island` 目录中创建一个 `styles` 文件夹,比如:
8+
9+
```bash
10+
.island
11+
├── config.ts
12+
└── styles
13+
└── index.css
14+
```
15+
16+
然后可以添加以下代码:
17+
18+
```css
19+
/* styles/index.css */
20+
:root {
21+
--island-c-brand: #f00;
22+
}
23+
```
24+
25+
这样 Island.js 会自动收集你在 `styles/index.css` 中定义的全局样式。

src/node/constants/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ export const ISLAND_CLI_PATH = join(CLI_BUNDLE_OUTDIR, 'cli.js');
5757
export const VENDOR_PATH = join(PACKAGE_ROOT_PATH, 'vendors');
5858

5959
export const DIRECTIVE_TYPES: string[] = ['tip', 'warning', 'danger', 'info'];
60+
61+
export const CUSTOM_GLOBAL_STYLE = 'virtual:custom-styles';
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CUSTOM_GLOBAL_STYLE } from '../../node/constants';
2+
import { Plugin } from 'vite';
3+
import { join } from 'path';
4+
import { pathExists } from 'fs-extra';
5+
import { SiteConfig } from 'shared/types';
6+
7+
export function pluginCustomStyle(config: SiteConfig): Plugin {
8+
return {
9+
name: 'island:custom-style',
10+
resolveId(id) {
11+
if (id === CUSTOM_GLOBAL_STYLE) {
12+
return `\0${CUSTOM_GLOBAL_STYLE}`;
13+
}
14+
},
15+
async load(id) {
16+
const stylePath = join(config.root!, '.island', 'styles', 'index.css');
17+
const styleExists = await pathExists(stylePath);
18+
19+
if (id === `\0${CUSTOM_GLOBAL_STYLE}`) {
20+
return styleExists ? `import '${stylePath}';` : '';
21+
}
22+
}
23+
};
24+
}

src/node/plugin-island/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SiteConfig } from 'shared/types';
22
import type { Plugin } from 'vite';
33
import { pluginConfig } from './config';
4+
import { pluginCustomStyle } from './custom-style';
45
import { pluginIndexHtml } from './indexHtml';
56
import { pluginIslandTransform } from './islandTransform';
67
import { pluginSiteData } from './siteDataPlugin';
@@ -14,6 +15,7 @@ export function pluginIsland(
1415
pluginSiteData(config),
1516
pluginConfig(config, restartServer),
1617
pluginIndexHtml(config),
17-
pluginIslandTransform(config, isServer)
18+
pluginIslandTransform(config, isServer),
19+
pluginCustomStyle(config)
1820
];
1921
}

src/runtime/client-entry.tsx

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import { ComponentType } from 'react';
21
import { setupEffects } from 'island/theme';
3-
4-
// Type shim for window.ISLANDS
5-
declare global {
6-
interface Window {
7-
ISLANDS: Record<string, ComponentType<unknown>>;
8-
// The state for island.
9-
ISLAND_PROPS: Record<string, unknown[]>;
10-
ISLAND_PAGE_DATA: unknown;
11-
}
12-
}
2+
import { ComponentType } from 'react';
133

144
async function renderInBrowser() {
155
const containerEl = document.getElementById('root');

src/runtime/hooks.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { createContext, useContext } from 'react';
1+
import { ComponentType, createContext, useContext } from 'react';
22
import { PageData } from '../shared/types';
33
import { inBrowser } from '../shared/utils';
44
import { routes } from 'virtual:routes';
55
import { Route } from 'node/plugin-routes';
66

7+
// Type shim for window.ISLANDS
8+
declare global {
9+
interface Window {
10+
ISLANDS: Record<string, ComponentType<unknown>>;
11+
// The state for island.
12+
ISLAND_PROPS: Record<string, unknown[]>;
13+
ISLAND_PAGE_DATA: unknown;
14+
}
15+
}
16+
717
export const DataContext = createContext({
818
data: inBrowser() ? window?.ISLAND_PAGE_DATA : null,
919
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars

src/shared/types/type.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ declare module '*.svg' {
5959
React.SVGProps<SVGSVGElement> & AttributifyAttributes
6060
>;
6161
export = ReactComponent;
62-
}
62+
}

src/theme-default/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './styles/base.css';
22
import './styles/vars.css';
33
import './styles/doc.css';
44
import 'uno.css';
5+
import 'virtual:custom-styles';
56
import { NotFoundLayout } from './layout/NotFountLayout/index';
67
import { Layout } from './layout/Layout';
78
import { HomeLayout } from './layout/HomeLayout/index';

src/theme-default/logic/useActive.ts

Whitespace-only changes.

src/theme-default/logic/useLocaleSiteData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DefaultTheme } from 'shared/types';
22
import { addLeadingSlash, removeTrailingSlash } from './index';
3-
import { usePageData } from '../../runtime';
3+
import { usePageData } from 'island/client';
44
import { useLocation } from 'react-router-dom';
55

66
export function useLocaleSiteData(): DefaultTheme.LocaleConfig {

0 commit comments

Comments
 (0)