Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Aug 11, 2024
1 parent 5e27af7 commit bb3147a
Show file tree
Hide file tree
Showing 30 changed files with 955 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"files": ["*.md"],
"options": {
"printWidth": 80
"printWidth": 100
}
}
]
Expand Down
204 changes: 204 additions & 0 deletions packages/gem-book/docs/en/003-plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
---
isNav: true
---

# Plugins

## `<gbp-code-group>`

Used to display several pieces of code with similar functionality:

<gbp-code-group>

```bash npm
npm i gem-book
```

```bash yarn
yarn add gem-book
```

</gbp-code-group>

````md
<gbp-code-group>

```bash npm
npm i gem-book
```

```bash yarn
yarn add gem-book
```

</gbp-code-group>
````

## `<gbp-raw>`

Used to display remote code. If the provided `src` only contains a path, it will read content from the current project's GitHub (affected by [`sourceDir`](./002-cli.md#--source-dir) and [`sourceBranch`](./002-cli.md#--source-branch)), for example:

<gbp-raw src="package.json" range="2-3,-4--6,author-license" highlight="-5,author"></gbp-raw>

```md
<!-- `range` specifies the display range, supporting negative numbers and string matching, `highlight` format is the same -->

<gbp-raw src="package.json" range="2-3,-4--6,author-license" highlight="-5,author"></gbp-raw>
```

## `<gbp-var>`

Reference global variable: <gbp-var>hello</gbp-var>

```md
<gbp-var>hello</gbp-var>
```

The variable needs to be defined in the [configuration file](./002-cli.md).

## `<gbp-media>`

Displays remote multimedia content, such as images or videos, using the same resource retrieval method as `<gbp-raw>`:

```md
<gbp-media src="preview.png"></gbp-media>
```

## `<gbp-include>`

Dynamically loads Markdown snippets:

<gbp-include src="./guide/007-extension.md" range="[!NOTE]->"></gbp-include>

```md
<!-- `range` syntax is the same as `<gbp-raw>`, here `range` uses string matching -->

<gbp-include src="./guide/007-extension.md" range="[!NOTE]->"></gbp-include>
```

## `<gbp-import>`

Dynamically imports modules, which can be used to load plugins on demand. For example, the following custom element is dynamically loaded (the `.ts` file will be compiled using [esm.sh](https://esm.sh/)):

<gbp-import src="docs/hello.ts"></gbp-import>

<my-plugin-hello></my-plugin-hello>

```md
<gbp-import src="docs/hello.ts"></gbp-import>

<my-plugin-hello></my-plugin-hello>
```

## `<gbp-content>`

Insert content into `<gem-book>`, allowing customization of `<gem-book>` content on specific pages, such as a custom sidebar:

```md
<gbp-content slot="sidebar-before">
<div>Test</div>
<style>
gem-book [part=sidebar-content] {
display: none;
}
</style>
</gbp-content>
```

## `<gbp-docsearch>`

Use [Algolia DocSearch](https://docsearch.algolia.com/) to provide search for the website, instantiated only once, and can be placed using [slots](./guide/007-extension.md#slots):

<gbp-raw src="docs/template.html" range="13--4"></gbp-raw>

> [!WARNING]
>
> `renderJavaScript` must be enabled in the [configuration](https://crawler.algolia.com/admin/crawlers) for Algolia DocSearch Crawler.
Using `docsearch?local` can provide local search service (thanks to [MiniSearch](https://github.com/lucaong/minisearch/)), [example](https://duoyun-ui.gemjs.org).

## `<gbp-comment>`

It uses [Gitalk](https://github.com/gitalk/gitalk) to bring comment functionality to the website, similar to the usage of `<gbp-docsearch>`:

```html
<gem-book>
<gbp-comment
slot="main-after"
client-id="xxx"
client-secret="xxx"
></gbp-comment>
</gem-book>
```

## `<gbp-sandpack>`

Use [Sandpack](https://sandpack.codesandbox.io/) to create interactive examples:

<gbp-sandpack dependencies="@mantou/gem, duoyun-ui">

```ts
import { render, html } from '@mantou/gem';

import 'duoyun-ui/elements/button';

render(
html`<dy-button>Time: ${new Date().toLocaleString()}</dy-button>`,
document.getElementById('root'),
);
```

</gbp-sandpack>

````md
<gbp-sandpack dependencies="@mantou/gem, duoyun-ui">

```ts
import { render, html } from '@mantou/gem';

import 'duoyun-ui/elements/button';

render(
html`<dy-button>Time: ${new Date().toLocaleString()}</dy-button>`,
document.getElementById('root'),
);
```

</gbp-sandpack>
````

## `<gbp-example>`

Generate examples for any custom element, for example:

<gbp-example name="dy-avatar" src="https://esm.sh/duoyun-ui/elements/avatar">

```json
{
"src": "https://api.dicebear.com/5.x/bottts-neutral/svg",
"status": "positive",
"size": "large",
"square": true
}
```

</gbp-example>

````md
<gbp-example name="dy-avatar" src="https://esm.sh/duoyun-ui/elements/avatar">

```json
{
"src": "https://api.dicebear.com/5.x/bottts-neutral/svg",
"status": "positive",
"size": "large",
"square": true
}
```

</gbp-example>
````

## `<gbp-api>`

Use [`gem-analyzer`](https://github.com/mantou132/gem/blob/main/packages/gem-analyzer) to generate API documentation for `GemElement`, such as [GemBookElement API](./004-api.md);
16 changes: 9 additions & 7 deletions packages/gem-examples/src/scope/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ const closedStyles = createCSSSheet(css`
`);
@shadow({ mode: 'closed' })
@adoptedStyle(closedStyles)
@customElement('app-closed')
@customElement('closed-shadow-dom')
class _Closed extends GemElement {
render() {
return html`<div>Closed shadow</div>`;
}
}

@customElement('other-element')
class _OtherElement extends GemElement {}
@customElement('light-dom')
class _OtherElement extends GemElement {
render() {
return html`<p>Other Content</p>`;
}
}

const style = createCSSSheet(css`
:scope {
Expand All @@ -36,13 +40,11 @@ export class App extends GemElement {
Text
<header><h1>Header</h1></header>
<p>Content</p>
<other-element>
<p>Other Content</p>
</other-element>
<light-dom></light-dom>
<article>
<p>Content</p>
</article>
<app-closed></app-closed>
<closed-shadow-dom></closed-shadow-dom>
`;
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/gem/docs/en/001-guide/001-basic/001-reactive-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ class MyElement extends GemElement {
> [!TIP]
> Do not modify prop/attr within the element, they should be passed in one-way by the parent element, just like native elements
In addition, `GemElement` provides React-like `state`/`setState` API to manage the state of the element itself. an element re-rendered is triggered whenever `setState` is called:
In addition, Gem provides the `createState` API to create the element's own state, and the created state object also acts as an update function, triggering the element to re-render when called.

```js
// Omit import...

@customElement('my-element')
class MyElement extends GemElement {
state = { id: 1 };
clicked() {
this.setState({ id: 2 });
#state = createState({ id: 1 });
#clicked() {
this.#state({ id: 2 });
}
render() {
return html`${this.state.id}`;
return html`${this.#state.id}`;
}
}
```
Expand Down Expand Up @@ -152,3 +152,6 @@ Complete life cycle:

> [!NOTE]
> The `constructor` and `unmounted` of the parent element are executed before the child element, but the `mounted` is executed after the child element
> [!WARNING]
> The lifecycle may be replaced in the future by decorators based on `@effect` `@memo` `@willMount` `@renderTemplate` `@mounted` `@unmounted
12 changes: 5 additions & 7 deletions packages/gem/docs/en/001-guide/001-basic/006-styled-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ You can reference CSS selectors in JS:

```js 17
import { GemElement, html } from '@mantou/gem';
import {
createCSSSheet,
styled,
adoptedStyle,
customElement,
} from '@mantou/gem';
import { createCSSSheet, styled, adoptedStyle, customElement } from '@mantou/gem';

const styles = createCSSSheet({
header: styled.class`
Expand All @@ -56,6 +51,9 @@ class MyElement extends GemElement {
}
```

> [!NOTE]
> Use `$` as a key to represent `:host, :scope` selectors, allowing styles to apply to both ShadowDOM and LightDOM.
## Customize the style outside the element

Use [`::part`](https://drafts.csswg.org/css-shadow-parts-1/#part)(only ShadowDOM) to export the internal content of the element, allowing external custom styles:
Expand Down Expand Up @@ -95,7 +93,7 @@ class MyElement extends GemElement {
```

> [!NOTE]
> Note the difference with `state`/`setState`
> Note the difference with `createState`
> [!TIP]
> Can also customize element styles using hack, for example:
Expand Down
4 changes: 4 additions & 0 deletions packages/gem/docs/en/001-guide/002-advance/003-theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ class App extends GemElement {
}
}
```

## Scoped and override theme

<gbp-include src="../../snippets/scoped-theme.md"></gbp-include>
5 changes: 5 additions & 0 deletions packages/gem/docs/en/001-guide/002-advance/010-debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ GemElement is a standard custom element that can be inspected and debugged in th
## Use Gem DevTools

![Gem DevTools](https://raw.githubusercontent.com/mantou132/gem/master/packages/gem-devtools/screenshot/firefox.jpg)

Features:

- Display all public non-standard members of the gem element, simple types allow editing
- When the non Gem element is selected, statistical analysis of all custom elements on the page will be displayed.
Loading

0 comments on commit bb3147a

Please sign in to comment.