-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
221 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Used in projects like React | ||
|
||
[Most front-end frameworks/libraries](https://custom-elements-everywhere.com/) can seamlessly use custom elements, such as assigning Attribute/Property, registering events, | ||
However, there is no [type hint](https://code.visualstudio.com/docs/editor/intellisense) when using custom elements directly, and ESLint is not supported, so DuoyunUI re-exported it and perfectly adapted to React/Vue/Svelte. | ||
|
||
## React | ||
|
||
> [!NOTE] | ||
> Only the experimental version of React supports custom elements. Use `npm install react@experimental react-dom@experimental` to install the experimental version of React. | ||
Use DuoyunUI like any other React component library: | ||
|
||
<gbp-raw range="3-19,31-" src="https://raw.githubusercontent.com/mantou132/nextjs-learn/main/pages/ce-test.tsx"></gbp-raw> | ||
|
||
## Vue | ||
|
||
DuoyunUI also exports Vue components, which are used the same as React. The only difference is that the path is changed from `react` to `vue`, | ||
in addition, custom elements need to be specified in the Vue configuration file: | ||
|
||
```js | ||
{ | ||
compilerOptions: { | ||
isCustomElement: (tag) => tag.startsWith('dy-'); | ||
} | ||
} | ||
``` | ||
|
||
In the Vue project, it also supports directly writing custom elements, but [distinguish](../02-elements/card#api) is it Attribute or Property: | ||
|
||
<gbp-raw codelang="html" range="34-45" src="https://raw.githubusercontent.com/mantou132/nuxtjs-learn/main/pages/test.vue"></gbp-raw> | ||
|
||
## Svelte | ||
|
||
DuoyunUI does not re-export as a Svelte component, and you can use the custom element directly: | ||
|
||
<gbp-raw codelang="html" range="2-9,46-57" src="https://raw.githubusercontent.com/mantou132/sveltekit-learn/main/src/routes/ce-test/+page.svelte"></gbp-raw> | ||
|
||
> [!NOTE] | ||
> Use the `Sveltekit`, please make sure the `Svelte` is installed as a `dependencies` instead of `DevDependenCies`, otherwise the type cannot be import successfully; | ||
> if you compile the error of "Unexpected token 'export'", please add the following code to `vite.config.ts`: | ||
> | ||
> ```js | ||
> { | ||
> ssr: { | ||
> noExternal: ['@mantou/gem', 'duoyun-ui']; | ||
> } | ||
> } | ||
> ``` | ||
## SSR | ||
DuoyunUI does not support SSR, to be precise, Next/Nuxt/Svelte do not support SSR for custom elements. The ShadowDOM of custom elements is generated at runtime. To ensure proper server-side rendering, import `@mantou/gem/helper/ssr-shim` at the entry point of your front-end code. | ||
- Next.js: `pages/_app.tsx` | ||
- Nuxt.js: `app.config.ts` | ||
- SvelteKit: `src/hooks.server.ts` | ||
If you want to use DuoyunUI routing, you can use `<dy-light-route>`. To avoid layout issues on the initial page load, add the following global styles: | ||
```css | ||
:not(:defined) { | ||
display: none; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 在 React 等项目中使用 | ||
|
||
[大多数前端框架/库](https://custom-elements-everywhere.com/)都能无缝使用自定义元素,比如分配 Attribute/Property,注册事件, | ||
但是直接使用自定义元素没有[类型提示](https://code.visualstudio.com/docs/editor/intellisense)、不支持 ESLint,所以 DuoyunUI 进行了重导出,完美适配了 React/Vue/Svelte。 | ||
|
||
## React | ||
|
||
> [!NOTE] | ||
> React 的实验版才支持自定义元素,使用 `npm install react@experimental react-dom@experimental` 安装 React 实验版。 | ||
跟使用其他 React 组件库一样使用 DuoyunUI: | ||
|
||
<gbp-raw range="3-19,31-" src="https://raw.githubusercontent.com/mantou132/nextjs-learn/main/pages/ce-test.tsx"></gbp-raw> | ||
|
||
## Vue | ||
|
||
DuoyunUI 也导出了 Vue 组件,使用和 React 一样,唯一的区别是路径将 `react` 改成 `vue`, | ||
另外需要在 Vue 配置文件中指定自定义元素: | ||
|
||
```js | ||
{ | ||
compilerOptions: { | ||
isCustomElement: (tag) => tag.startsWith('dy-'); | ||
} | ||
} | ||
``` | ||
|
||
在 Vue 中也支持直接写自定义元素,但是要[区分](../02-elements/card#api)是 Attribute 还是 Property: | ||
|
||
<gbp-raw codelang="html" range="34-45" src="https://raw.githubusercontent.com/mantou132/nuxtjs-learn/main/pages/test.vue"></gbp-raw> | ||
|
||
## Svelte | ||
|
||
DuoyunUI 没有重导出为 Svelte 组件,直接使用自定义元素即可: | ||
|
||
<gbp-raw codelang="html" range="2-9,46-57" src="https://raw.githubusercontent.com/mantou132/sveltekit-learn/main/src/routes/ce-test/+page.svelte"></gbp-raw> | ||
|
||
> [!NOTE] | ||
> 使用 `SvelteKit` 请确保 `svelte` 安装成 `dependencies` 而非 `devDependencies`,否则类型不能成功导入; | ||
> 如果编译出现“Unexpected token 'export'”的错误请在 `vite.config.ts` 中添加下面代码: | ||
> | ||
> ```js | ||
> { | ||
> ssr: { | ||
> noExternal: ['@mantou/gem', 'duoyun-ui']; | ||
> } | ||
> } | ||
> ``` | ||
## SSR | ||
DuoyunUI 不支持 SSR,确切的说是 Next/Nuxt/Svelte 不支持自定义元素 SSR,自定义元素的 ShadowDOM 是运行时生成, | ||
为了能在服务端正确运行,在前端代码的入口位置导入 `@mantou/gem/helper/ssr-shim`: | ||
- Next.js: `pages/_app.tsx` | ||
- Nuxt.js: `app.config.ts` | ||
- SvelteKit: `src/hooks.server.ts` | ||
如果要使用 DuoyunUI 的路由,可以使用 `<dy-light-route>`。为了避免首屏排版错误添加下面全局样式: | ||
```css | ||
:not(:defined) { | ||
display: none; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { Path } from '../store'; | ||
|
||
export const setValue = (path: Path, value: string | number | boolean | null) => { | ||
declare let $0: any; | ||
|
||
export const setGemPropValue = (path: Path, value: string | number | boolean | null) => { | ||
const key = String(path.pop()); | ||
|
||
const obj = window.__GEM_DEVTOOLS__PRELOAD__.readProp(path); | ||
|
||
obj[key] = value; | ||
|
||
$0.update(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters