Skip to content

Commit

Permalink
feat(blockquote-directive-svg-to-data-image): add all template result
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarmarina committed Dec 30, 2022
1 parent bcf489c commit 9813b4e
Show file tree
Hide file tree
Showing 8 changed files with 1,246 additions and 810 deletions.
113 changes: 113 additions & 0 deletions packages/directives/blockquote-directive-svg-to-data-image/README.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
# blockquoteDirectiveSvgToDataImage
![Lit](https://img.shields.io/badge/lit-2.0.0-blue)
This directive receives an `<svg>` HTML element from a [Template Result](https://lit.dev/docs/api/templates/#TemplateResult) and returns it as data URI (data:image/svg+xml).
> [svg-templates](https://lit.dev/tutorials/svg-templates/#1)
## Usage:
### Simple SVG
```js
get _svgTag() {
return html` <svg
xmlns="htt://www.w3.org/2000/svg"
viewBox="0 0 100 100"
enable-background="new 0 0 100 100"
xml:space="preserve"
height="100px"
width="100px"
>
<g>
<path
d="M28.1,36.6c4.6,1.9,12.2,1.6,20.9,1.1c8.9-0.4,19-0.9,28.9,0.9c6.3,1.2,11.9,3.1,16.8,6c-1.5-12.2-7.9-23.7-18.6-31.3 c-4.9-0.2-9.9,0.3-14.8,1.4C47.8,17.9,36.2,25.6,28.1,36.6z"
/>
<path
d="M70.3,9.8C57.5,3.4,42.8,3.6,30.5,9.5c-3,6-8.4,19.6-5.3,24.9c8.6-11.7,20.9-19.8,35.2-23.1C63.7,10.5,67,10,70.3,9.8z"
/>
<path
d="M16.5,51.3c0.6-1.7,1.2-3.4,2-5.1c-3.8-3.4-7.5-7-11-10.8c-2.1,6.1-2.8,12.5-2.3,18.7C9.6,51.1,13.4,50.2,16.5,51.3z"
/>
<path
d="M9,31.6c3.5,3.9,7.2,7.6,11.1,11.1c0.8-1.6,1.7-3.1,2.6-4.6c0.1-0.2,0.3-0.4,0.4-0.6c-2.9-3.3-3.1-9.2-0.6-17.6 c0.8-2.7,1.8-5.3,2.7-7.4c-5.2,3.4-9.8,8-13.3,13.7C10.8,27.9,9.8,29.7,9,31.6z"
/>
<path
d="M15.4,54.7c-2.6-1-6.1,0.7-9.7,3.4c1.2,6.6,3.9,13,8,18.5C13,69.3,13.5,61.8,15.4,54.7z"
/>
<path
d="M39.8,57.6C54.3,66.7,70,73,86.5,76.4c0.6-0.8,1.1-1.6,1.7-2.5c4.8-7.7,7-16.3,6.8-24.8c-13.8-9.3-31.3-8.4-45.8-7.7 c-9.5,0.5-17.8,0.9-23.2-1.7c-0.1,0.1-0.2,0.3-0.3,0.4c-1,1.7-2,3.4-2.9,5.1C28.2,49.7,33.8,53.9,39.8,57.6z"
/>
<path
d="M26.2,88.2c3.3,2,6.7,3.6,10.2,4.7c-3.5-6.2-6.3-12.6-8.8-18.5c-3.1-7.2-5.8-13.5-9-17.2c-1.9,8-2,16.4-0.3,24.7 C20.6,84.2,23.2,86.3,26.2,88.2z"
/>
<path
d="M30.9,73c2.9,6.8,6.1,14.4,10.5,21.2c15.6,3,32-2.3,42.6-14.6C67.7,76,52.2,69.6,37.9,60.7C32,57,26.5,53,21.3,48.6 c-0.6,1.5-1.2,3-1.7,4.6C24.1,57.1,27.3,64.5,30.9,73z"
/>
</g>
</svg>`;
}
render() {
return html`
<div
aria-hidden="true"
style="background-image: url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}');"
></div>
`;
}
```
### Complex SVG
```js
get _bgTpl() {
return svg` <circle fill="${this.bgFill}" cx="100" cy="100" r="100"></circle> `;
}
get _shapeTpl() {
return svg`
<g fill="#fff">
<circle cx="100" cy="35.819" r="14.926"></circle>
<path
d="M164.771 50.73a7.459 7.459 0 0 0-8.814-5.802L100 56.465 44.043 44.928a7.46 7.46 0 0 0-8.815 5.802 7.461 7.461 0 0 0 5.803 8.815l41.805 8.62v45.563l-11.782 56.387a7.46 7.46 0 0 0 5.778 8.83c.515.107 1.028.16 1.535.16a7.467 7.467 0 0 0 7.297-5.938l12.015-57.498c.224-1.188 1.086-2.237 2.323-2.237 1.236 0 2.123 1.049 2.322 2.237l12.016 57.498a7.464 7.464 0 0 0 8.831 5.778 7.458 7.458 0 0 0 5.776-8.83l-11.781-56.391V68.166l41.807-8.62a7.46 7.46 0 0 0 5.798-8.816z"
></path>
</g>
`;
}
get _svgTag() {
return html`<svg
id="${Math.random().toString(36).substring(2, 10)}"
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
focusable="false"
role="presentation"
viewBox="0 0 200 200"
>
${this._bgTpl} ${this._shapeTpl}
</svg>`;
}
render() {
return html`
<div
aria-hidden="true"
style="background-image:var(--bgUrl, url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}'));"
></div>
`;
}
```
## Exports
- blockquoteDirectiveSvgToDataImage
@tagname blockquote-directive-vg-to-data-image
@element blockquote-directive-vg-to-data-image
*/
export class ReadmElement extends HTMLElement {}
103 changes: 89 additions & 14 deletions packages/directives/blockquote-directive-svg-to-data-image/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,107 @@
# blockquote-directive-vg-to-data-image

# blockquoteDirectiveSvgToDataImage

![Lit](https://img.shields.io/badge/lit-2.0.0-blue)

This directive receives a [Lit SVG Template Result](https://lit.dev/docs/api/templates/#svg) (created using the `svg` tagged template literal) and returns a data URI string.
This directive receives an `<svg>` HTML element from a [Template Result](https://lit.dev/docs/api/templates/#TemplateResult) and returns it as data URI (data:image/svg+xml).

> [svg-templates](https://lit.dev/tutorials/svg-templates/#1)
## Usage:

### Simple SVG

```js
get _svgTag() {
return svg`<svg id="${Math.random()
.toString(36)
.substring(2,10)}" width="51" height="51" xmlns="http://www.w3.org/2000/svg"><path d="M25.5 0C11.417 0 0 11.417 0 25.5S11.417 51 25.5 51 51 39.583 51 25.5 39.583 0 25.5 0zm-.385 5.064a3.3 3.3 0 0 1 3.307 3.291 3.304 3.304 0 0 1-3.307 3.306 3.3 3.3 0 0 1-3.29-3.306 3.29 3.29 0 0 1 3.29-3.291zm14.289 10.652l-9.809 1.24.005 9.817 4.755 15.867a1.85 1.85 0 0 1-1.344 2.252c-.989.25-2.003-.3-2.252-1.298l-4.87-14.443h-1.498l-4.48 14.742c-.374.964-1.448 1.404-2.407 1.03-.954-.37-1.533-1.454-1.158-2.418l4.115-15.572v-9.978l-9.04-1.228c-.928-.075-1.558-.89-1.483-1.818.07-.934.914-1.628 1.838-1.554l10.982.944h4.815l11.69-.963a1.68 1.68 0 0 1 1.749 1.623c.04.924-.68 1.718-1.608 1.758z"></path></svg>`;
return html` <svg
xmlns="htt://www.w3.org/2000/svg"
viewBox="0 0 100 100"
enable-background="new 0 0 100 100"
xml:space="preserve"
height="100px"
width="100px"
>
<g>
<path
d="M28.1,36.6c4.6,1.9,12.2,1.6,20.9,1.1c8.9-0.4,19-0.9,28.9,0.9c6.3,1.2,11.9,3.1,16.8,6c-1.5-12.2-7.9-23.7-18.6-31.3 c-4.9-0.2-9.9,0.3-14.8,1.4C47.8,17.9,36.2,25.6,28.1,36.6z"
/>
<path
d="M70.3,9.8C57.5,3.4,42.8,3.6,30.5,9.5c-3,6-8.4,19.6-5.3,24.9c8.6-11.7,20.9-19.8,35.2-23.1C63.7,10.5,67,10,70.3,9.8z"
/>
<path
d="M16.5,51.3c0.6-1.7,1.2-3.4,2-5.1c-3.8-3.4-7.5-7-11-10.8c-2.1,6.1-2.8,12.5-2.3,18.7C9.6,51.1,13.4,50.2,16.5,51.3z"
/>
<path
d="M9,31.6c3.5,3.9,7.2,7.6,11.1,11.1c0.8-1.6,1.7-3.1,2.6-4.6c0.1-0.2,0.3-0.4,0.4-0.6c-2.9-3.3-3.1-9.2-0.6-17.6 c0.8-2.7,1.8-5.3,2.7-7.4c-5.2,3.4-9.8,8-13.3,13.7C10.8,27.9,9.8,29.7,9,31.6z"
/>
<path
d="M15.4,54.7c-2.6-1-6.1,0.7-9.7,3.4c1.2,6.6,3.9,13,8,18.5C13,69.3,13.5,61.8,15.4,54.7z"
/>
<path
d="M39.8,57.6C54.3,66.7,70,73,86.5,76.4c0.6-0.8,1.1-1.6,1.7-2.5c4.8-7.7,7-16.3,6.8-24.8c-13.8-9.3-31.3-8.4-45.8-7.7 c-9.5,0.5-17.8,0.9-23.2-1.7c-0.1,0.1-0.2,0.3-0.3,0.4c-1,1.7-2,3.4-2.9,5.1C28.2,49.7,33.8,53.9,39.8,57.6z"
/>
<path
d="M26.2,88.2c3.3,2,6.7,3.6,10.2,4.7c-3.5-6.2-6.3-12.6-8.8-18.5c-3.1-7.2-5.8-13.5-9-17.2c-1.9,8-2,16.4-0.3,24.7 C20.6,84.2,23.2,86.3,26.2,88.2z"
/>
<path
d="M30.9,73c2.9,6.8,6.1,14.4,10.5,21.2c15.6,3,32-2.3,42.6-14.6C67.7,76,52.2,69.6,37.9,60.7C32,57,26.5,53,21.3,48.6 c-0.6,1.5-1.2,3-1.7,4.6C24.1,57.1,27.3,64.5,30.9,73z"
/>
</g>
</svg>`;
}

render() {
return html`
<div
aria-hidden="true"
style="background-image: url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}');"
></div>
`;
return html`
<div
aria-hidden="true"
style="background-image: url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}');"
></div>
`;
}
```

```html
<div
style="background-image:url('data:image/svg+xml;charset=utf-8,%3Csvg%20id=%229jcuff16%22%20width=%2251%22%20height=%2251%22%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3Cpath%20d=%22M25.5%200C11.417%200%200%2011.417%200%2025.5S11.417%2051%2025.5%2051%2051%2039.583%2051%2025.5%2039.583%200%2025.5%200zm-.385%205.064a3.3%203.3%200%200%201%203.307%203.291%203.304%203.304%200%200%201-3.307%203.306%203.3%203.3%200%200%201-3.29-3.306%203.29%203.29%200%200%201%203.29-3.291zm14.289%2010.652l-9.809%201.24.005%209.817%204.755%2015.867a1.85%201.85%200%200%201-1.344%202.252c-.989.25-2.003-.3-2.252-1.298l-4.87-14.443h-1.498l-4.48%2014.742c-.374.964-1.448%201.404-2.407%201.03-.954-.37-1.533-1.454-1.158-2.418l4.115-15.572v-9.978l-9.04-1.228c-.928-.075-1.558-.89-1.483-1.818.07-.934.914-1.628%201.838-1.554l10.982.944h4.815l11.69-.963a1.68%201.68%200%200%201%201.749%201.623c.04.924-.68%201.718-1.608%201.758z%22%3E%3C/path%3E%3C/svg%3E')"
></div>

### Complex SVG

```js
get _bgTpl() {
return svg` <circle fill="${this.bgFill}" cx="100" cy="100" r="100"></circle> `;
}

get _shapeTpl() {
return svg`
<g fill="#fff">
<circle cx="100" cy="35.819" r="14.926"></circle>
<path
d="M164.771 50.73a7.459 7.459 0 0 0-8.814-5.802L100 56.465 44.043 44.928a7.46 7.46 0 0 0-8.815 5.802 7.461 7.461 0 0 0 5.803 8.815l41.805 8.62v45.563l-11.782 56.387a7.46 7.46 0 0 0 5.778 8.83c.515.107 1.028.16 1.535.16a7.467 7.467 0 0 0 7.297-5.938l12.015-57.498c.224-1.188 1.086-2.237 2.323-2.237 1.236 0 2.123 1.049 2.322 2.237l12.016 57.498a7.464 7.464 0 0 0 8.831 5.778 7.458 7.458 0 0 0 5.776-8.83l-11.781-56.391V68.166l41.807-8.62a7.46 7.46 0 0 0 5.798-8.816z"
></path>
</g>
`;
}

get _svgTag() {
return html`<svg
id="${Math.random().toString(36).substring(2, 10)}"
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
focusable="false"
role="presentation"
viewBox="0 0 200 200"
>
${this._bgTpl} ${this._shapeTpl}
</svg>`;
}

render() {
return html`
<div
aria-hidden="true"
style="background-image:var(--bgUrl, url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}'));"
></div>
`;
}
```

## Exports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<meta charset="utf-8" />
<style>
:root {
/* https://stackoverflow.com/questions/42330075/is-there-a-way-to-interpolate-css-variables-with-url#answer-42331003 */
/* --bgUrl : url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve" height="100px" width="100px">%0A<g>%0A%09<path d="M28.1,36.6c4.6,1.9,12.2,1.6,20.9,1.1c8.9-0.4,19-0.9,28.9,0.9c6.3,1.2,11.9,3.1,16.8,6c-1.5-12.2-7.9-23.7-18.6-31.3 c-4.9-0.2-9.9,0.3-14.8,1.4C47.8,17.9,36.2,25.6,28.1,36.6z"/>%0A%09<path d="M70.3,9.8C57.5,3.4,42.8,3.6,30.5,9.5c-3,6-8.4,19.6-5.3,24.9c8.6-11.7,20.9-19.8,35.2-23.1C63.7,10.5,67,10,70.3,9.8z"/>%0A%09<path d="M16.5,51.3c0.6-1.7,1.2-3.4,2-5.1c-3.8-3.4-7.5-7-11-10.8c-2.1,6.1-2.8,12.5-2.3,18.7C9.6,51.1,13.4,50.2,16.5,51.3z"/>%0A%09<path d="M9,31.6c3.5,3.9,7.2,7.6,11.1,11.1c0.8-1.6,1.7-3.1,2.6-4.6c0.1-0.2,0.3-0.4,0.4-0.6c-2.9-3.3-3.1-9.2-0.6-17.6 c0.8-2.7,1.8-5.3,2.7-7.4c-5.2,3.4-9.8,8-13.3,13.7C10.8,27.9,9.8,29.7,9,31.6z"/>%0A%09<path d="M15.4,54.7c-2.6-1-6.1,0.7-9.7,3.4c1.2,6.6,3.9,13,8,18.5C13,69.3,13.5,61.8,15.4,54.7z"/>%0A%09<path d="M39.8,57.6C54.3,66.7,70,73,86.5,76.4c0.6-0.8,1.1-1.6,1.7-2.5c4.8-7.7,7-16.3,6.8-24.8c-13.8-9.3-31.3-8.4-45.8-7.7 c-9.5,0.5-17.8,0.9-23.2-1.7c-0.1,0.1-0.2,0.3-0.3,0.4c-1,1.7-2,3.4-2.9,5.1C28.2,49.7,33.8,53.9,39.8,57.6z"/>%0A%09<path d="M26.2,88.2c3.3,2,6.7,3.6,10.2,4.7c-3.5-6.2-6.3-12.6-8.8-18.5c-3.1-7.2-5.8-13.5-9-17.2c-1.9,8-2,16.4-0.3,24.7 C20.6,84.2,23.2,86.3,26.2,88.2z"/>%0A%09<path d="M30.9,73c2.9,6.8,6.1,14.4,10.5,21.2c15.6,3,32-2.3,42.6-14.6C67.7,76,52.2,69.6,37.9,60.7C32,57,26.5,53,21.3,48.6 c-0.6,1.5-1.2,3-1.7,4.6C24.1,57.1,27.3,64.5,30.9,73z"/>%0A</g>%0A</svg>'); */

font: normal medium/1.25 sans-serif;
}

Expand All @@ -30,14 +33,32 @@
<body>
<main>
<h1 class="sr-only">blockquote-directive-svg-to-data-image</h1>
<bg-element></bg-element>
<bg-element bg-fill="hsl(0deg, 0%, 13%, 0.98)"></bg-element>
</main>

<script type="module">
import { html, LitElement, css, svg } from 'lit';
import { blockquoteDirectiveSvgToDataImage } from '../index.js';

class BgElement extends LitElement {
static get properties() {
return {
/**
* svg - background color
* @type {String}
*/
bgFill: {
type: 'String',
attribute: 'bg-fill',
},
};
}

constructor() {
super();
this.bgFill = 'rgb(32, 32, 32)';
}

static get styles() {
return css`
:host {
Expand Down Expand Up @@ -81,13 +102,33 @@ <h1 class="sr-only">blockquote-directive-svg-to-data-image</h1>
`;
}

get _bgTpl() {
return svg` <circle fill="${this.bgFill}" cx="100" cy="100" r="100"></circle> `;
}

get _shapeTpl() {
return svg`
<g fill="#fff">
<circle cx="100" cy="35.819" r="14.926"></circle>
<path
d="M164.771 50.73a7.459 7.459 0 0 0-8.814-5.802L100 56.465 44.043 44.928a7.46 7.46 0 0 0-8.815 5.802 7.461 7.461 0 0 0 5.803 8.815l41.805 8.62v45.563l-11.782 56.387a7.46 7.46 0 0 0 5.778 8.83c.515.107 1.028.16 1.535.16a7.467 7.467 0 0 0 7.297-5.938l12.015-57.498c.224-1.188 1.086-2.237 2.323-2.237 1.236 0 2.123 1.049 2.322 2.237l12.016 57.498a7.464 7.464 0 0 0 8.831 5.778 7.458 7.458 0 0 0 5.776-8.83l-11.781-56.391V68.166l41.807-8.62a7.46 7.46 0 0 0 5.798-8.816z"
></path>
</g>
`;
}

get _svgTag() {
return svg`<svg id="${Math.random()
.toString(36)
.substring(
2,
10,
)}" xmlns="http://www.w3.org/2000/svg" width="200" height="200" focusable="false" role="presentation" viewBox="0 0 200 200"><circle fill="#202020" cx="100" cy="100" r="100"></circle><g fill="#fff"><circle cx="100" cy="35.819" r="14.926"></circle><path d="M164.771 50.73a7.459 7.459 0 0 0-8.814-5.802L100 56.465 44.043 44.928a7.46 7.46 0 0 0-8.815 5.802 7.461 7.461 0 0 0 5.803 8.815l41.805 8.62v45.563l-11.782 56.387a7.46 7.46 0 0 0 5.778 8.83c.515.107 1.028.16 1.535.16a7.467 7.467 0 0 0 7.297-5.938l12.015-57.498c.224-1.188 1.086-2.237 2.323-2.237 1.236 0 2.123 1.049 2.322 2.237l12.016 57.498a7.464 7.464 0 0 0 8.831 5.778 7.458 7.458 0 0 0 5.776-8.83l-11.781-56.391V68.166l41.807-8.62a7.46 7.46 0 0 0 5.798-8.816z"></path></g></svg>`;
return html`<svg
id="${Math.random().toString(36).substring(2, 10)}"
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
focusable="false"
role="presentation"
viewBox="0 0 200 200"
>
${this._bgTpl} ${this._shapeTpl}
</svg>`;
}

render() {
Expand All @@ -96,7 +137,8 @@ <h1 class="sr-only">blockquote-directive-svg-to-data-image</h1>
target="_blank"
rel="noopener noreferrer"
href="https://accessibility-club.org/"
style="background-image: url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}');"
style="background-image:var(--bgUrl,
url('${blockquoteDirectiveSvgToDataImage(this._svgTag)}'));"
>
<span class="sr-only">Accessibility Club</span>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<body>
<blockquote-base-embedded-webview heading="blockquote-directive-svg-to-data-image">
<template data-src="./base.html" data-option="Base"></template>
<template data-src="./simple.html" data-option="Simple"></template>
</blockquote-base-embedded-webview>
<script type="module">
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down
Loading

0 comments on commit 9813b4e

Please sign in to comment.