Skip to content

Commit 3061534

Browse files
authored
feat: add only property to filter elements (#153)
1 parent 552fb67 commit 3061534

File tree

8 files changed

+44
-7
lines changed

8 files changed

+44
-7
lines changed

.changeset/rare-numbers-hide.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@api-viewer/common': patch
3+
'@api-viewer/demo': patch
4+
'@api-viewer/docs': patch
5+
'api-viewer-element': patch
6+
'@api-viewer/tabs': patch
7+
---
8+
9+
Add only property to filter elements

docs/docs/api/properties.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ Use `manifest` property instead of `src` to pass manifest data directly:
3131
</script>
3232
```
3333

34+
### `only`
35+
36+
Use `only` to display API only for one or a few elements in the scope of a certain documentation page and filter out the rest.
37+
38+
```html
39+
<api-viewer src="./custom-elements.json" only="my-el,my-other-el"></api-viewer>
40+
```
41+
3442
### `selected`
3543

3644
Use `selected` property to configure the displayed element.

packages/api-common/src/manifest-mixin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export interface ManifestMixinInterface {
1010

1111
manifest?: Package;
1212

13+
only?: string[];
14+
1315
selected?: string;
1416

1517
jsonFetched: Promise<Package | null>;
@@ -28,6 +30,15 @@ export const ManifestMixin = <T extends Constructor<LitElement>>(
2830
@property({ attribute: false })
2931
manifest?: Package;
3032

33+
@property({
34+
reflect: true,
35+
converter: {
36+
fromAttribute: (value: string) => value.split(','),
37+
toAttribute: (value: string[]) => value.join(',')
38+
}
39+
})
40+
only?: string[];
41+
3142
@property() selected?: string;
3243

3344
jsonFetched: Promise<Package | null> = Promise.resolve(null);

packages/api-common/src/manifest.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ export async function fetchManifest(src: string): Promise<Package | null> {
6565
}
6666
}
6767

68-
export function getCustomElements(manifest: Package): CustomElementExport[] {
69-
return (manifest.modules ?? []).flatMap(
68+
export function getCustomElements(
69+
manifest: Package,
70+
only?: string[]
71+
): CustomElementExport[] {
72+
const exports = (manifest.modules ?? []).flatMap(
7073
(x) => x.exports?.filter(isCustomElementExport) ?? []
7174
);
75+
return only ? exports.filter((e) => only.includes(e.name)) : exports;
7276
}
7377

7478
export const getElementData = (

packages/api-demo/src/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import './layout.js';
1616
async function renderDemo(
1717
jsonFetched: Promise<Package | null>,
1818
onSelect: (e: CustomEvent) => void,
19+
only?: string[],
1920
selected?: string,
2021
id?: number,
2122
exclude = ''
@@ -26,7 +27,7 @@ async function renderDemo(
2627
return emptyDataWarning;
2728
}
2829

29-
const elements = getCustomElements(manifest);
30+
const elements = getCustomElements(manifest, only);
3031

3132
const data = getElementData(manifest, selected) as CustomElement;
3233
const props = getPublicFields(data.members);
@@ -81,6 +82,7 @@ export class ApiDemoBase extends ManifestMixin(LitElement) {
8182
renderDemo(
8283
this.jsonFetched,
8384
this._onSelect,
85+
this.only,
8486
this.selected,
8587
this._id,
8688
this.excludeKnobs

packages/api-docs/src/base.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import './layout.js';
1717
async function renderDocs(
1818
jsonFetched: Promise<Package | null>,
1919
onSelect: (e: CustomEvent) => void,
20+
only?: string[],
2021
selected?: string
2122
): Promise<TemplateResult> {
2223
const manifest = await jsonFetched;
@@ -25,7 +26,7 @@ async function renderDocs(
2526
return emptyDataWarning;
2627
}
2728

28-
const elements = getCustomElements(manifest);
29+
const elements = getCustomElements(manifest, only);
2930

3031
const data = getElementData(manifest, selected) as CustomElement;
3132
const props = getPublicFields(data.members);
@@ -69,7 +70,7 @@ async function renderDocs(
6970
export class ApiDocsBase extends ManifestMixin(LitElement) {
7071
protected render(): TemplateResult {
7172
return html`${until(
72-
renderDocs(this.jsonFetched, this._onSelect, this.selected)
73+
renderDocs(this.jsonFetched, this._onSelect, this.only, this.selected)
7374
)}`;
7475
}
7576

packages/api-viewer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"size-limit": [
6060
{
6161
"path": "lib/api-viewer.js",
62-
"limit": "38.6 KB"
62+
"limit": "38.7 KB"
6363
}
6464
]
6565
}

packages/api-viewer/src/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async function renderDocs(
2323
section: string,
2424
onSelect: (e: CustomEvent) => void,
2525
onToggle: (e: CustomEvent) => void,
26+
only?: string[],
2627
selected?: string,
2728
id?: number,
2829
exclude = ''
@@ -33,7 +34,7 @@ async function renderDocs(
3334
return emptyDataWarning;
3435
}
3536

36-
const elements = getCustomElements(manifest);
37+
const elements = getCustomElements(manifest, only);
3738

3839
const data = getElementData(manifest, selected) as CustomElement;
3940
const props = getPublicFields(data.members);
@@ -134,6 +135,7 @@ export class ApiViewerBase extends ManifestMixin(LitElement) {
134135
this.section,
135136
this._onSelect,
136137
this._onToggle,
138+
this.only,
137139
this.selected,
138140
this._id,
139141
this.excludeKnobs

0 commit comments

Comments
 (0)