Skip to content

Commit

Permalink
feat: add only property to filter elements (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashmish authored Apr 7, 2022
1 parent 552fb67 commit 3061534
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .changeset/rare-numbers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@api-viewer/common': patch
'@api-viewer/demo': patch
'@api-viewer/docs': patch
'api-viewer-element': patch
'@api-viewer/tabs': patch
---

Add only property to filter elements
8 changes: 8 additions & 0 deletions docs/docs/api/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ Use `manifest` property instead of `src` to pass manifest data directly:
</script>
```

### `only`

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.

```html
<api-viewer src="./custom-elements.json" only="my-el,my-other-el"></api-viewer>
```

### `selected`

Use `selected` property to configure the displayed element.
Expand Down
11 changes: 11 additions & 0 deletions packages/api-common/src/manifest-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface ManifestMixinInterface {

manifest?: Package;

only?: string[];

selected?: string;

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

@property({
reflect: true,
converter: {
fromAttribute: (value: string) => value.split(','),
toAttribute: (value: string[]) => value.join(',')
}
})
only?: string[];

@property() selected?: string;

jsonFetched: Promise<Package | null> = Promise.resolve(null);
Expand Down
8 changes: 6 additions & 2 deletions packages/api-common/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ export async function fetchManifest(src: string): Promise<Package | null> {
}
}

export function getCustomElements(manifest: Package): CustomElementExport[] {
return (manifest.modules ?? []).flatMap(
export function getCustomElements(
manifest: Package,
only?: string[]
): CustomElementExport[] {
const exports = (manifest.modules ?? []).flatMap(
(x) => x.exports?.filter(isCustomElementExport) ?? []
);
return only ? exports.filter((e) => only.includes(e.name)) : exports;
}

export const getElementData = (
Expand Down
4 changes: 3 additions & 1 deletion packages/api-demo/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import './layout.js';
async function renderDemo(
jsonFetched: Promise<Package | null>,
onSelect: (e: CustomEvent) => void,
only?: string[],
selected?: string,
id?: number,
exclude = ''
Expand All @@ -26,7 +27,7 @@ async function renderDemo(
return emptyDataWarning;
}

const elements = getCustomElements(manifest);
const elements = getCustomElements(manifest, only);

const data = getElementData(manifest, selected) as CustomElement;
const props = getPublicFields(data.members);
Expand Down Expand Up @@ -81,6 +82,7 @@ export class ApiDemoBase extends ManifestMixin(LitElement) {
renderDemo(
this.jsonFetched,
this._onSelect,
this.only,
this.selected,
this._id,
this.excludeKnobs
Expand Down
5 changes: 3 additions & 2 deletions packages/api-docs/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import './layout.js';
async function renderDocs(
jsonFetched: Promise<Package | null>,
onSelect: (e: CustomEvent) => void,
only?: string[],
selected?: string
): Promise<TemplateResult> {
const manifest = await jsonFetched;
Expand All @@ -25,7 +26,7 @@ async function renderDocs(
return emptyDataWarning;
}

const elements = getCustomElements(manifest);
const elements = getCustomElements(manifest, only);

const data = getElementData(manifest, selected) as CustomElement;
const props = getPublicFields(data.members);
Expand Down Expand Up @@ -69,7 +70,7 @@ async function renderDocs(
export class ApiDocsBase extends ManifestMixin(LitElement) {
protected render(): TemplateResult {
return html`${until(
renderDocs(this.jsonFetched, this._onSelect, this.selected)
renderDocs(this.jsonFetched, this._onSelect, this.only, this.selected)
)}`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/api-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"size-limit": [
{
"path": "lib/api-viewer.js",
"limit": "38.6 KB"
"limit": "38.7 KB"
}
]
}
4 changes: 3 additions & 1 deletion packages/api-viewer/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function renderDocs(
section: string,
onSelect: (e: CustomEvent) => void,
onToggle: (e: CustomEvent) => void,
only?: string[],
selected?: string,
id?: number,
exclude = ''
Expand All @@ -33,7 +34,7 @@ async function renderDocs(
return emptyDataWarning;
}

const elements = getCustomElements(manifest);
const elements = getCustomElements(manifest, only);

const data = getElementData(manifest, selected) as CustomElement;
const props = getPublicFields(data.members);
Expand Down Expand Up @@ -134,6 +135,7 @@ export class ApiViewerBase extends ManifestMixin(LitElement) {
this.section,
this._onSelect,
this._onToggle,
this.only,
this.selected,
this._id,
this.excludeKnobs
Expand Down

0 comments on commit 3061534

Please sign in to comment.