@patrick91/stickers 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ <script lang="ts">
2
+ import { mountSticker } from '../index.js';
3
+ import { useStickers } from './context.js';
4
+ import type { StickerProps } from './types.js';
5
+
6
+ let { src, width, height, alt = '', rotate = 0, finish, cut, border, fillHoles, id, crossOrigin, class: className, style }: StickerProps = $props();
7
+ let image = $state<HTMLImageElement>();
8
+ let anchor = $state<HTMLSpanElement>();
9
+ const engine = useStickers();
10
+
11
+ $effect(() => {
12
+ if (!image) return;
13
+ return mountSticker($engine, image, { anchor, width, rotation: rotate, finish, cut, border, fillHoles, id });
14
+ });
15
+ </script>
16
+
17
+ <span bind:this={anchor} class={className} {style} style:--sticker-width={`${width}px`}>
18
+ <img bind:this={image} crossorigin={crossOrigin} {src} {alt} {width} {height} draggable="false" style:transform={`rotate(${rotate}deg)`} />
19
+ </span>
20
+
21
+ <style>
22
+ span {
23
+ display: inline-block;
24
+ width: var(--sticker-width);
25
+ max-width: 100%;
26
+ line-height: 0;
27
+ user-select: none;
28
+ }
29
+
30
+ img {
31
+ display: block;
32
+ width: 100%;
33
+ height: auto;
34
+ pointer-events: none;
35
+ }
36
+ </style>
@@ -0,0 +1,4 @@
1
+ import type { StickerProps } from './types.js';
2
+ declare const Sticker: import("svelte").Component<StickerProps, {}, "">;
3
+ type Sticker = ReturnType<typeof Sticker>;
4
+ export default Sticker;
@@ -0,0 +1,23 @@
1
+ <script lang="ts">
2
+ import { onMount, setContext } from 'svelte';
3
+ import { writable } from 'svelte/store';
4
+ import { createStickerEngine } from '../index.js';
5
+ import type { StickerEngine } from '../index.js';
6
+ import { engineContext } from './context.js';
7
+ import type { StickerProviderProps } from './types.js';
8
+
9
+ let { children, ...options }: StickerProviderProps = $props();
10
+ const engine = writable<StickerEngine | null>(null);
11
+ setContext(engineContext, engine);
12
+
13
+ onMount(() => {
14
+ const created = createStickerEngine({ ...options });
15
+ engine.set(created);
16
+ return () => {
17
+ engine.set(null);
18
+ created?.destroy();
19
+ };
20
+ });
21
+ </script>
22
+
23
+ {@render children?.()}
@@ -0,0 +1,4 @@
1
+ import type { StickerProviderProps } from './types.js';
2
+ declare const StickerProvider: import("svelte").Component<StickerProviderProps, {}, "">;
3
+ type StickerProvider = ReturnType<typeof StickerProvider>;
4
+ export default StickerProvider;
@@ -0,0 +1,5 @@
1
+ import type { Readable } from 'svelte/store';
2
+ import type { StickerEngine } from '../index.js';
3
+ export declare const engineContext: unique symbol;
4
+ /** A readable store containing the nearest provider's engine, or null before mount. */
5
+ export declare function useStickers(): Readable<StickerEngine | null>;
@@ -0,0 +1,8 @@
1
+ import { getContext } from 'svelte';
2
+ import { readable } from 'svelte/store';
3
+ export const engineContext = Symbol('stickers');
4
+ const noEngine = readable(null);
5
+ /** A readable store containing the nearest provider's engine, or null before mount. */
6
+ export function useStickers() {
7
+ return getContext(engineContext) ?? noEngine;
8
+ }
@@ -0,0 +1,4 @@
1
+ export { default as Sticker } from './Sticker.svelte';
2
+ export { default as StickerProvider } from './StickerProvider.svelte';
3
+ export { useStickers } from './context.js';
4
+ export type { StickerProps, StickerProviderProps } from './types.js';
@@ -0,0 +1,3 @@
1
+ export { default as Sticker } from './Sticker.svelte';
2
+ export { default as StickerProvider } from './StickerProvider.svelte';
3
+ export { useStickers } from './context.js';
@@ -0,0 +1,9 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { EngineOptions, StickerImageProps } from '../index.js';
3
+ export interface StickerProviderProps extends EngineOptions {
4
+ children?: Snippet;
5
+ }
6
+ export interface StickerProps extends StickerImageProps {
7
+ class?: string;
8
+ style?: string;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { EngineOptions, StickerEngine } from './index.js';
2
+ export interface StickerProviderElement extends HTMLElement {
3
+ /** Set before connecting the element. A persist attribute overrides options.persist. */
4
+ options?: EngineOptions;
5
+ readonly engine: StickerEngine | null;
6
+ reset(): void;
7
+ setExploded(on: boolean): void;
8
+ }
9
+ declare global {
10
+ interface HTMLElementTagNameMap {
11
+ 'peel-sticker-provider': StickerProviderElement;
12
+ 'peel-sticker': HTMLElement;
13
+ }
14
+ }
15
+ /** Register the two custom elements explicitly. Safe to import on the server and to call twice. */
16
+ export declare function defineStickerElements(): void;
@@ -0,0 +1,123 @@
1
+ import { createStickerEngine, mountSticker } from './index.js';
2
+ const PROVIDER_TAG = 'peel-sticker-provider';
3
+ const STICKER_TAG = 'peel-sticker';
4
+ function numberAttribute(element, name) {
5
+ const value = element.getAttribute(name);
6
+ if (value === null || value.trim() === '')
7
+ return;
8
+ const number = Number(value);
9
+ return Number.isFinite(number) ? number : undefined;
10
+ }
11
+ function mountSlot(host, css) {
12
+ const document = host.ownerDocument;
13
+ const style = document.createElement('style');
14
+ style.textContent = css;
15
+ const slot = document.createElement('slot');
16
+ host.attachShadow({ mode: 'open' }).append(style, slot);
17
+ return slot;
18
+ }
19
+ /** Register the two custom elements explicitly. Safe to import on the server and to call twice. */
20
+ export function defineStickerElements() {
21
+ if (typeof customElements === 'undefined' || typeof HTMLElement === 'undefined')
22
+ return;
23
+ if (!customElements.get(PROVIDER_TAG)) {
24
+ customElements.define(PROVIDER_TAG, class extends HTMLElement {
25
+ #engine = null;
26
+ constructor() {
27
+ super();
28
+ mountSlot(this, ':host { display: contents; }');
29
+ }
30
+ connectedCallback() {
31
+ const persist = this.getAttribute('persist');
32
+ this.#engine = createStickerEngine(persist === null ? this.options : { ...this.options, persist });
33
+ }
34
+ disconnectedCallback() {
35
+ this.#engine?.destroy();
36
+ this.#engine = null;
37
+ }
38
+ get engine() { return this.#engine; }
39
+ reset() { this.#engine?.reset(); }
40
+ setExploded(on) { this.#engine?.setExploded(on); }
41
+ });
42
+ }
43
+ if (!customElements.get(STICKER_TAG)) {
44
+ customElements.define(STICKER_TAG, class extends HTMLElement {
45
+ static observedAttributes = ['rotate', 'finish', 'cut', 'border', 'fill-holes', 'sticker-id'];
46
+ #dispose;
47
+ #queued = false;
48
+ #observer = new MutationObserver(() => this.#schedule());
49
+ constructor() {
50
+ super();
51
+ const slot = mountSlot(this, `
52
+ :host {
53
+ display: inline-block;
54
+ width: var(--peel-width, auto);
55
+ max-width: 100%;
56
+ line-height: 0;
57
+ user-select: none;
58
+ }
59
+ ::slotted(img) {
60
+ display: block;
61
+ width: 100%;
62
+ height: auto;
63
+ pointer-events: none;
64
+ transform: rotate(var(--peel-rotation, 0deg));
65
+ }
66
+ ::slotted(picture) { display: block; }
67
+ `);
68
+ slot.addEventListener('slotchange', () => this.#schedule());
69
+ }
70
+ connectedCallback() {
71
+ this.#observer.observe(this, { subtree: true, attributes: true, attributeFilter: ['width'] });
72
+ this.#schedule();
73
+ }
74
+ disconnectedCallback() {
75
+ this.#observer.disconnect();
76
+ this.#dispose?.();
77
+ this.#dispose = undefined;
78
+ }
79
+ attributeChangedCallback() {
80
+ this.#schedule();
81
+ }
82
+ #schedule() {
83
+ if (this.#queued || !this.isConnected)
84
+ return;
85
+ this.#queued = true;
86
+ queueMicrotask(() => {
87
+ this.#queued = false;
88
+ if (this.isConnected)
89
+ this.#mount();
90
+ });
91
+ }
92
+ #mount() {
93
+ this.#dispose?.();
94
+ this.#dispose = undefined;
95
+ // The artwork is the img, on its own or in a picture that offers it in other formats.
96
+ const image = this.querySelector(':scope > img, :scope > picture > img');
97
+ const widthAttr = image ? numberAttribute(image, 'width') : undefined;
98
+ const width = widthAttr !== undefined && widthAttr > 0 ? widthAttr : undefined;
99
+ if (width === undefined)
100
+ this.style.removeProperty('--peel-width');
101
+ else
102
+ this.style.setProperty('--peel-width', `${width}px`);
103
+ const rotation = numberAttribute(this, 'rotate') ?? 0;
104
+ this.style.setProperty('--peel-rotation', `${rotation}deg`);
105
+ const engine = this.closest(PROVIDER_TAG)?.engine ?? null;
106
+ if (!image || !engine)
107
+ return;
108
+ const cut = this.getAttribute('cut');
109
+ const border = numberAttribute(this, 'border');
110
+ this.#dispose = mountSticker(engine, image, {
111
+ anchor: this,
112
+ width,
113
+ rotation,
114
+ finish: (this.getAttribute('finish') ?? undefined),
115
+ cut: cut !== null && ['contour', 'circle', 'rect', 'rounded'].includes(cut) ? cut : undefined,
116
+ border: border === undefined ? undefined : Math.max(0, border),
117
+ fillHoles: this.hasAttribute('fill-holes'),
118
+ id: this.getAttribute('sticker-id') ?? undefined,
119
+ });
120
+ }
121
+ });
122
+ }
123
+ }
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@patrick91/stickers",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.1",
4
+ "description": "Peelable WebGL stickers for plain JavaScript, Web Components, React, and Svelte.",
5
+ "keywords": ["stickers", "peel", "drag", "webgl", "webgl2", "web-components", "custom-elements", "react", "svelte"],
6
+ "license": "MIT",
7
+ "author": "Patrick Arminio (https://patrick.wtf)",
8
+ "homepage": "https://stickers.patrick.wtf",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/patrick91/stickers.git",
12
+ "directory": "packages/stickers"
13
+ },
14
+ "type": "module",
15
+ "sideEffects": ["**/*.css"],
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./web-components": {
23
+ "types": "./dist/web-components.d.ts",
24
+ "import": "./dist/web-components.js"
25
+ },
26
+ "./react": {
27
+ "types": "./dist/react.d.ts",
28
+ "import": "./dist/react.js"
29
+ },
30
+ "./svelte": {
31
+ "types": "./dist/svelte/index.d.ts",
32
+ "svelte": "./dist/svelte/index.js",
33
+ "import": "./dist/svelte/index.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "CHANGELOG.md"
39
+ ],
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc && node scripts/copy-engine.mjs && svelte-package --input src/svelte --output dist/svelte --tsconfig tsconfig.svelte.json 1>&2",
45
+ "check": "svelte-check --tsconfig tsconfig.svelte.json",
46
+ "prepack": "npm run build"
47
+ },
48
+ "peerDependencies": {
49
+ "react": "^18.3.1 || ^19.0.0",
50
+ "svelte": "^5.0.0"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "react": { "optional": true },
54
+ "svelte": { "optional": true }
55
+ },
56
+ "devDependencies": {
57
+ "@sveltejs/package": "^3.0.3",
58
+ "@types/react": "^18.3.0",
59
+ "svelte": "^5.0.0",
60
+ "svelte-check": "^4.7.9"
61
+ }
62
+ }