Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 361a5e5

Browse files
koddssonkeithamus
andcommitted
Add README plugin and generate README
Co-authored-by: Keith Cirkel <[email protected]>
1 parent df056bd commit 361a5e5

File tree

6 files changed

+132
-43
lines changed

6 files changed

+132
-43
lines changed

README.md

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
# &lt;details-dialog&gt; element
22

3-
A modal dialog that's opened with a &lt;details&gt; button.
3+
A modal dialog opened with a <details> button.
44

55
## Installation
6-
7-
```
8-
$ npm install --save @github/details-dialog-element
9-
```
6+
Available on [npm](https://www.npmjs.com/) as [**@github/details-dialog-element**](https://www.npmjs.com/package/@github/details-dialog-element).
7+
```
8+
$ npm install --save @github/details-dialog-element
9+
```
1010
1111
## Usage
1212
13-
### Script
14-
15-
Import as ES modules:
16-
17-
```js
18-
import '@github/details-dialog-element'
19-
```
20-
21-
Include with a script tag:
22-
23-
```html
24-
<script type="module" src="./node_modules/@github/details-dialog-element/dist/index.js">
25-
```
13+
### details-dialog
2614
2715
### Markup
2816
@@ -74,26 +62,11 @@ document.addEventListener('details-dialog-close', function(event) {
7462
})
7563
```
7664

77-
## Browser support
78-
79-
Browsers without native [custom element support][support] require a [polyfill][].
80-
81-
- Chrome
82-
- Firefox
83-
- Safari
84-
- Microsoft Edge
85-
86-
## Development
87-
88-
```
89-
npm install
90-
npm test
91-
```
92-
93-
## License
94-
95-
Distributed under the MIT license. See LICENSE for details.
96-
97-
[fragment]: https://github.com/github/include-fragment-element/
98-
[support]: https://caniuse.com/#feat=custom-elementsv1
99-
[polyfill]: https://github.com/webcomponents/custom-elements
65+
## Browser Support
66+
Browsers without native [custom element support][support] require a [polyfill][].
67+
- Chrome
68+
- Firefox
69+
- Safari
70+
- Microsoft Edge
71+
[support]: https://caniuse.com/custom-elementsv1
72+
[polyfill]: https://github.com/webcomponents/custom-elements

cem-plugin-readme.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {accessSync, readFileSync, writeFileSync} from 'fs'
2+
3+
function generateInstallationInstructions({packageJson: {name}}) {
4+
return `## Installation
5+
Available on [npm](https://www.npmjs.com/) as [**${name}**](https://www.npmjs.com/package/${name}).
6+
\`\`\`
7+
$ npm install --save ${name}
8+
\`\`\``
9+
}
10+
11+
function generateBrowserSupportInstructions() {
12+
return `## Browser Support
13+
Browsers without native [custom element support][support] require a [polyfill][].
14+
- Chrome
15+
- Firefox
16+
- Safari
17+
- Microsoft Edge
18+
[support]: https://caniuse.com/custom-elementsv1
19+
[polyfill]: https://github.com/webcomponents/custom-elements`
20+
}
21+
22+
function generateTitle({packageJson: {name}}) {
23+
const formattedName = name
24+
.split('/')
25+
.at(-1)
26+
.replace(/-element$/, '')
27+
return `&lt;${formattedName}&gt; element`
28+
}
29+
30+
function generateDescription({packageJson: {description}}) {
31+
return description
32+
}
33+
34+
export function readme(options) {
35+
const {filename = 'README.md', exclude = [], title, preamble, footer} = options ?? {}
36+
37+
const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'))
38+
39+
return {
40+
name: 'readme',
41+
async packageLinkPhase({customElementsManifest}) {
42+
const content = [
43+
`# ${title || generateTitle({packageJson})}`,
44+
generateDescription({packageJson}),
45+
preamble,
46+
generateInstallationInstructions({packageJson}),
47+
`## Usage`
48+
]
49+
for (const module of customElementsManifest.modules) {
50+
for (const {name, tagName, description} of module.declarations.filter(x => x.customElement)) {
51+
if (exclude.includes(name)) continue
52+
content.push(`### ${tagName}`)
53+
content.push(description.trim())
54+
}
55+
}
56+
57+
content.push(generateBrowserSupportInstructions())
58+
content.push(footer)
59+
60+
writeFileSync(filename, content.filter(Boolean).join('\n\n'))
61+
}
62+
}
63+
}

custom-elements-manifest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {generateCustomData} from 'cem-plugin-vs-code-custom-data-generator'
2+
import {readme} from './cem-plugin-readme.js'
23

34
export default {
45
packagejson: true,
56
globs: ['src/index.ts'],
67
plugins: [
8+
readme(),
79
generateCustomData()
810
]
911
}

custom-elements.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"declarations": [
99
{
1010
"kind": "class",
11-
"description": "",
11+
"description": "### Markup\n\n```html\n<details>\n <summary>Open dialog</summary>\n <details-dialog>\n Modal content\n <button type=\"button\" data-close-dialog>Close</button>\n </details-dialog>\n</details>\n```\n\n## Deferred loading\n\nDialog content can be loaded from a server by embedding an [`<include-fragment>`][fragment] element.\n\n```html\n<details>\n <summary>Robots</summary>\n <details-dialog src=\"/robots\" preload>\n <include-fragment>Loading…</include-fragment>\n </details-dialog>\n</details>\n```\n\nThe `src` attribute value is copied to the `<include-fragment>` the first time the `<details>` button is toggled open, which starts the server fetch.\n\nIf the `preload` attribute is present, hovering over the `<details>` element will trigger the server fetch.\n\n## Events\n\n### `details-dialog-close`\n\n`details-dialog-close` event is fired from `<details-dialog>` when a request to close the dialog is made from\n\n- pressing <kbd>escape</kbd>,\n- submitting a `form[method=\"dialog\"]`\n- clicking on `summary, form button[formmethod=\"dialog\"], [data-close-dialog]`, or\n- `dialog.toggle(false)`\n\nThis event bubbles, and can be canceled to keep the dialog open.\n\n```js\ndocument.addEventListener('details-dialog-close', function(event) {\n if (!confirm('Are you sure?')) {\n event.preventDefault()\n }\n})\n```",
1212
"name": "DetailsDialogElement",
1313
"members": [
1414
{

src/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,57 @@ type State = {
181181

182182
const initialized: WeakMap<Element, State> = new WeakMap()
183183

184+
/**
185+
* ### Markup
186+
*
187+
* ```html
188+
* <details>
189+
* <summary>Open dialog</summary>
190+
* <details-dialog>
191+
* Modal content
192+
* <button type="button" data-close-dialog>Close</button>
193+
* </details-dialog>
194+
* </details>
195+
* ```
196+
*
197+
* ## Deferred loading
198+
*
199+
* Dialog content can be loaded from a server by embedding an [`<include-fragment>`][fragment] element.
200+
*
201+
* ```html
202+
* <details>
203+
* <summary>Robots</summary>
204+
* <details-dialog src="/robots" preload>
205+
* <include-fragment>Loading…</include-fragment>
206+
* </details-dialog>
207+
* </details>
208+
* ```
209+
*
210+
* The `src` attribute value is copied to the `<include-fragment>` the first time the `<details>` button is toggled open, which starts the server fetch.
211+
*
212+
* If the `preload` attribute is present, hovering over the `<details>` element will trigger the server fetch.
213+
*
214+
* ## Events
215+
*
216+
* ### `details-dialog-close`
217+
*
218+
* `details-dialog-close` event is fired from `<details-dialog>` when a request to close the dialog is made from
219+
*
220+
* - pressing <kbd>escape</kbd>,
221+
* - submitting a `form[method="dialog"]`
222+
* - clicking on `summary, form button[formmethod="dialog"], [data-close-dialog]`, or
223+
* - `dialog.toggle(false)`
224+
*
225+
* This event bubbles, and can be canceled to keep the dialog open.
226+
*
227+
* ```js
228+
* document.addEventListener('details-dialog-close', function(event) {
229+
* if (!confirm('Are you sure?')) {
230+
* event.preventDefault()
231+
* }
232+
* })
233+
* ```
234+
**/
184235
class DetailsDialogElement extends HTMLElement {
185236
static get CLOSE_ATTR() {
186237
return CLOSE_ATTR

vscode.html-custom-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"tags": [
33
{
44
"name": "details-dialog",
5-
"description": "",
5+
"description": "### Markup\n\n```html\n<details>\n <summary>Open dialog</summary>\n <details-dialog>\n Modal content\n <button type=\"button\" data-close-dialog>Close</button>\n </details-dialog>\n</details>\n```\n\n## Deferred loading\n\nDialog content can be loaded from a server by embedding an [`<include-fragment>`][fragment] element.\n\n```html\n<details>\n <summary>Robots</summary>\n <details-dialog src=\"/robots\" preload>\n <include-fragment>Loading…</include-fragment>\n </details-dialog>\n</details>\n```\n\nThe `src` attribute value is copied to the `<include-fragment>` the first time the `<details>` button is toggled open, which starts the server fetch.\n\nIf the `preload` attribute is present, hovering over the `<details>` element will trigger the server fetch.\n\n## Events\n\n### `details-dialog-close`\n\n`details-dialog-close` event is fired from `<details-dialog>` when a request to close the dialog is made from\n\n- pressing <kbd>escape</kbd>,\n- submitting a `form[method=\"dialog\"]`\n- clicking on `summary, form button[formmethod=\"dialog\"], [data-close-dialog]`, or\n- `dialog.toggle(false)`\n\nThis event bubbles, and can be canceled to keep the dialog open.\n\n```js\ndocument.addEventListener('details-dialog-close', function(event) {\n if (!confirm('Are you sure?')) {\n event.preventDefault()\n }\n})\n```",
66
"attributes": [{ "name": "src" }, { "name": "preload" }]
77
}
88
]

0 commit comments

Comments
 (0)