diff --git a/src/SvgToInline.stories.ts b/src/SvgToInline.stories.ts index 5e69da3..b45b05c 100644 --- a/src/SvgToInline.stories.ts +++ b/src/SvgToInline.stories.ts @@ -5,8 +5,9 @@ import './SvgToInline.ts' interface ArgTypes { path: string className: string - lazy: boolean - slot?: TemplateResult | string + placeholder?: TemplateResult + lazy?: boolean + 'loading-label'?: TemplateResult } export default { @@ -14,13 +15,31 @@ export default { tags: ['autodocs'], component: 'svg-to-inline', argTypes: { - path: { control: 'text', description: 'SVG path' }, + path: { + control: 'text', + type: { required: true }, + description: 'SVG path', + }, className: { control: 'text', table: { type: { summary: 'string' }, }, }, + 'loading-label': { + control: 'text', + description: 'While loading the element to be visible', + table: { + type: { summary: 'string' }, + }, + }, + placeholder: { + control: 'text', + description: 'If fetch error the element to be visible', + table: { + type: { summary: 'string' }, + }, + }, lazy: { control: 'boolean', description: ` @@ -40,11 +59,23 @@ interface Story { argTypes?: Record } -const Template: Story = ({ path, className }: ArgTypes) => html` - +const Template: Story = ({ + path, + className, + 'loading-label': loadingLabel, + placeholder, +}: ArgTypes) => html` + ` export const Regular = Template.bind({}) Regular.args = { - path: './SVG_Logo.svg', + path: './SVG_Logo.svg.', + 'loading-label': html``, + placeholder: html``, } diff --git a/src/SvgToInline.test.ts b/src/SvgToInline.test.ts index 376621f..82d3024 100644 --- a/src/SvgToInline.test.ts +++ b/src/SvgToInline.test.ts @@ -4,31 +4,29 @@ import type { SvgToInline } from './SvgToInline.js' import './SvgToInline.js' describe('SvgToInline', () => { + // it('test _ and ; value') + // it('test path') + // it('loading') + // it('placeholder') + // it('lazy') // it('has a default header "Hey there" and counter 5', async () => { // const el = await fixture(html``) - // expect(el.header).to.equal('Hey there') // expect(el.counter).to.equal(5) // }) - // it('increases the counter on button click', async () => { // const el = await fixture(html``) // el.shadowRoot!.querySelector('button')!.click() - // expect(el.counter).to.equal(6) // }) - // it('can override the header via attribute', async () => { // const el = await fixture( - // html``, + // html``, // ) - - // expect(el.header).to.equal('attribute header') + // expect(el.path).to.equal('attribute header') // }) - it('passes the a11y audit', async () => { const el = await fixture(html``) - await expect(el).shadowDom.to.be.accessible() }) }) diff --git a/src/SvgToInline.ts b/src/SvgToInline.ts index 015f9da..ec760ea 100644 --- a/src/SvgToInline.ts +++ b/src/SvgToInline.ts @@ -1,4 +1,5 @@ import { html, LitElement } from 'lit' +import type { TemplateResult } from 'lit' import { state, customElement, property } from 'lit/decorators.js' import { throttle } from 'throttle-debounce' import { fetchFile } from './utils/fetchFile.js' @@ -10,18 +11,27 @@ export class SvgToInline extends LitElement { @state() private _svgDOM: string | null = null + @state() + private _statusElement?: TemplateResult | null + @state() private _throttleLazyFetchSVG: (event: Event) => void @property() path?: string - @property({ type: Boolean }) - lazy: boolean = false - @property() className: string = '' + @property({ type: Object, attribute: 'loading-label' }) + loading?: TemplateResult + + @property({ type: Object }) + placeholder?: TemplateResult + + @property({ type: Boolean }) + lazy: boolean = false + constructor() { super() @@ -61,9 +71,17 @@ export class SvgToInline extends LitElement { private async _fetchSVG() { if (this.path) { + if (this.loading) { + this._statusElement = this.loading + } const svgString = await fetchFile(this.path) - this._svgDOM = svgString + if (svgString) { + this._svgDOM = svgString + } else { + this._svgDOM = null + this._statusElement = this.placeholder + } } } @@ -95,6 +113,6 @@ export class SvgToInline extends LitElement { render() { return html`${this._svgDOM ? convertStringToNode(addClassNames(this._svgDOM, this.className)) - : html``}` + : this._statusElement}` } } diff --git a/src/utils/fetchFile.ts b/src/utils/fetchFile.ts index d158dca..e082e1b 100644 --- a/src/utils/fetchFile.ts +++ b/src/utils/fetchFile.ts @@ -1,9 +1,15 @@ export const fetchFile = async (path: string) => { try { - const response = await (await fetch(path)).text() - return response + const response = await fetch(path) + + if (!response.ok) { + return null + } + + const data = await response.text() + return data + // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { - console.error(error) return null } }