@uibit/scratch-reveal 0.1.1 → 0.2.0

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.
@@ -1,34 +1,37 @@
1
1
  import { Component, ChangeDetectionStrategy, ElementRef, input, effect, output, numberAttribute } from '@angular/core';
2
2
  import '@uibit/scratch-reveal';
3
3
  import type { ScratchReveal as HTMLElementClass } from '@uibit/scratch-reveal';
4
-
5
4
  @Component({
6
- selector: 'uibit-scratch-reveal',
7
- template: '<ng-content></ng-content>',
8
- changeDetection: ChangeDetectionStrategy.OnPush,
9
- standalone: true,
10
- host: {
11
- '(scratch-progress)': 'scratchProgress.emit($event)',
12
- '(scratch-reveal)': 'scratchReveal.emit($event)'
13
- }
5
+ selector: 'uibit-scratch-reveal',
6
+ template: '<ng-content></ng-content>',
7
+ changeDetection: ChangeDetectionStrategy.OnPush,
8
+ standalone: true,
9
+ host: {
10
+ '(scratch-progress)': 'scratchProgress.emit($event)',
11
+ '(scratch-reveal)': 'scratchReveal.emit($event)'
12
+ }
14
13
  })
15
14
  export class NgxScratchReveal {
16
- constructor(private el: ElementRef<HTMLElementClass>) {
17
- effect(() => {
18
- if (this.el.nativeElement) {
19
- this.el.nativeElement.revealThreshold = this.revealThreshold();
20
- }
15
+ constructor(private el: ElementRef<HTMLElementClass>){
16
+ effect(()=>{
17
+ if (this.el.nativeElement) {
18
+ this.el.nativeElement.revealThreshold = this.revealThreshold();
19
+ }
20
+ });
21
+ effect(()=>{
22
+ if (this.el.nativeElement) {
23
+ this.el.nativeElement.locale = this.locale();
24
+ }
25
+ });
26
+ }
27
+ readonly revealThreshold = input<number, any>(50, {
28
+ transform: numberAttribute
21
29
  });
22
- effect(() => {
23
- if (this.el.nativeElement) {
24
- this.el.nativeElement.locale = this.locale();
25
- }
26
- });
27
- }
28
-
29
- readonly revealThreshold = input<number, any>(50, { transform: numberAttribute });
30
- readonly locale = input<string, any>('');
31
-
32
- readonly scratchProgress = output<CustomEvent<{ percentage: number }>>();
33
- readonly scratchReveal = output<CustomEvent<{ percentage: number }>>();
30
+ readonly locale = input<string>('');
31
+ readonly scratchProgress = output<CustomEvent<{
32
+ percentage: number;
33
+ }>>();
34
+ readonly scratchReveal = output<CustomEvent<{
35
+ percentage: number;
36
+ }>>();
34
37
  }
@@ -4,7 +4,12 @@ import '@uibit/scratch-reveal';
4
4
  declare global {
5
5
  namespace astroHTML.JSX {
6
6
  interface IntrinsicElements {
7
- 'uibit-scratch-reveal': Partial<HTMLElementClass> & astroHTML.JSX.HTMLAttributes;
7
+ 'uibit-scratch-reveal': Partial<HTMLElementClass> & astroHTML.JSX.HTMLAttributes & {
8
+ revealThreshold?: number;
9
+ locale?: string;
10
+ "on:scratch-progress"?: (event: CustomEvent<{ percentage: number }>) => void;
11
+ "on:scratch-reveal"?: (event: CustomEvent<{ percentage: number }>) => void;
12
+ };
8
13
  }
9
14
  }
10
15
  }
@@ -8,6 +8,10 @@ declare module 'preact' {
8
8
  'uibit-scratch-reveal': JSX.HTMLAttributes<HTMLElementClass> & {
9
9
  revealThreshold?: number;
10
10
  locale?: string;
11
+ onScratchProgress?: (event: CustomEvent<{ percentage: number }>) => void;
12
+ "on:scratch-progress"?: (event: CustomEvent<{ percentage: number }>) => void;
13
+ onScratchReveal?: (event: CustomEvent<{ percentage: number }>) => void;
14
+ "on:scratch-reveal"?: (event: CustomEvent<{ percentage: number }>) => void;
11
15
  };
12
16
  }
13
17
  }
@@ -0,0 +1,101 @@
1
+ import React, { useRef, useEffect, useLayoutEffect, useImperativeHandle } from 'react';
2
+ import type { ScratchReveal as HTMLElementClass } from '@uibit/scratch-reveal';
3
+ import '@uibit/scratch-reveal';
4
+ const useTypeOfLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
5
+ export interface ScratchRevealProps extends Omit<React.HTMLAttributes<HTMLElementClass>, 'revealThreshold' | 'locale' | 'onScratchProgress' | 'onScratchReveal'> {
6
+ children?: React.ReactNode;
7
+ revealThreshold?: number;
8
+ locale?: string;
9
+ onScratchProgress?: (event: CustomEvent<{
10
+ percentage: number;
11
+ }>) => void;
12
+ onScratchReveal?: (event: CustomEvent<{
13
+ percentage: number;
14
+ }>) => void;
15
+ }
16
+ export const ScratchReveal = ({ ref, children, ...props }: ScratchRevealProps & {
17
+ ref?: React.Ref<HTMLElementClass>;
18
+ })=>{
19
+ const innerRef = useRef<HTMLElementClass>(null);
20
+ const propsRef = useRef(props);
21
+ propsRef.current = props;
22
+ useImperativeHandle(ref, ()=>innerRef.current!);
23
+ useTypeOfLayoutEffect(()=>{
24
+ const element = innerRef.current;
25
+ if (element && props.revealThreshold !== undefined) {
26
+ (element as any).revealThreshold = props.revealThreshold;
27
+ }
28
+ }, [
29
+ props.revealThreshold
30
+ ]);
31
+ useTypeOfLayoutEffect(()=>{
32
+ const element = innerRef.current;
33
+ if (element && props.locale !== undefined) {
34
+ (element as any).locale = props.locale;
35
+ }
36
+ }, [
37
+ props.locale
38
+ ]);
39
+ useTypeOfLayoutEffect(()=>{
40
+ const element = innerRef.current;
41
+ if (!element) return;
42
+ const handleEvent = (event: Event)=>{
43
+ if (propsRef.current.onScratchProgress) {
44
+ propsRef.current.onScratchProgress(event as any);
45
+ }
46
+ };
47
+ element.addEventListener('scratch-progress', handleEvent);
48
+ return ()=>{
49
+ element.removeEventListener('scratch-progress', handleEvent);
50
+ };
51
+ }, []);
52
+ useTypeOfLayoutEffect(()=>{
53
+ const element = innerRef.current;
54
+ if (!element) return;
55
+ const handleEvent = (event: Event)=>{
56
+ if (propsRef.current.onScratchReveal) {
57
+ propsRef.current.onScratchReveal(event as any);
58
+ }
59
+ };
60
+ element.addEventListener('scratch-reveal', handleEvent);
61
+ return ()=>{
62
+ element.removeEventListener('scratch-reveal', handleEvent);
63
+ };
64
+ }, []);
65
+ const domProps = {
66
+ ...props
67
+ };
68
+ const customPropNames = [
69
+ 'revealThreshold',
70
+ 'locale',
71
+ 'onScratchProgress',
72
+ 'onScratchReveal'
73
+ ];
74
+ for (const key of customPropNames){
75
+ delete (domProps as any)[key];
76
+ }
77
+ return React.createElement('uibit-scratch-reveal', {
78
+ ref: innerRef,
79
+ ...domProps
80
+ }, children);
81
+ };
82
+ declare global {
83
+ namespace React {
84
+ namespace JSX {
85
+ interface IntrinsicElements {
86
+ 'uibit-scratch-reveal': React.ClassAttributes<HTMLElementClass> & React.HTMLAttributes<HTMLElementClass> & {
87
+ children?: React.ReactNode;
88
+ class?: string;
89
+ revealThreshold?: number;
90
+ locale?: string;
91
+ onScratchProgress?: (event: CustomEvent<{
92
+ percentage: number;
93
+ }>) => void;
94
+ onScratchReveal?: (event: CustomEvent<{
95
+ percentage: number;
96
+ }>) => void;
97
+ };
98
+ }
99
+ }
100
+ }
101
+ }
@@ -8,8 +8,8 @@ declare module 'solid-js' {
8
8
  'uibit-scratch-reveal': Partial<HTMLElementClass> & JSX.HTMLAttributes<HTMLElementClass> & {
9
9
  revealThreshold?: number;
10
10
  locale?: string;
11
- "on:scratch-progress"?: (event: CustomEvent) => void;
12
- "on:scratch-reveal"?: (event: CustomEvent) => void;
11
+ "on:scratch-progress"?: (event: CustomEvent<{ percentage: number }>) => void;
12
+ "on:scratch-reveal"?: (event: CustomEvent<{ percentage: number }>) => void;
13
13
  };
14
14
  }
15
15
  }
@@ -7,6 +7,10 @@ declare module '@stencil/core' {
7
7
  'uibit-scratch-reveal': HTMLElementClass & {
8
8
  revealThreshold?: number;
9
9
  locale?: string;
10
+ onScratchProgress?: (event: CustomEvent<{ percentage: number }>) => void;
11
+ "on:scratch-progress"?: (event: CustomEvent<{ percentage: number }>) => void;
12
+ onScratchReveal?: (event: CustomEvent<{ percentage: number }>) => void;
13
+ "on:scratch-reveal"?: (event: CustomEvent<{ percentage: number }>) => void;
10
14
  };
11
15
  }
12
16
  }
@@ -1,32 +1,58 @@
1
1
  <script lang="ts">
2
2
  import '@uibit/scratch-reveal';
3
3
  import type { ScratchReveal as HTMLElementClass } from '@uibit/scratch-reveal';
4
-
5
- let {
6
- revealThreshold = undefined,
7
- locale = undefined,
8
- children
4
+
5
+ let {
6
+ revealThreshold = $bindable(),
7
+ locale = $bindable(),
8
+ children,
9
+ ...restProps
9
10
  } = $props<{
10
- children?: any;
11
+ children?: import('svelte').Snippet;
11
12
  revealThreshold?: number;
12
13
  locale?: string;
13
-
14
+
15
+ [key: string]: any;
14
16
  }>();
15
-
16
- let elementRef: HTMLElementClass | null = $state(null);
17
-
18
- $effect(() => {
19
- if (elementRef && revealThreshold !== undefined) {
17
+
18
+ let elementRef: HTMLElementClass | null = $state(null);
19
+
20
+ $effect(() => {
21
+ if (elementRef && revealThreshold !== undefined && elementRef.revealThreshold !== revealThreshold) {
20
22
  elementRef.revealThreshold = revealThreshold;
21
23
  }
22
- if (elementRef && locale !== undefined) {
24
+ if (elementRef && locale !== undefined && elementRef.locale !== locale) {
23
25
  elementRef.locale = locale;
24
26
  }
25
27
  });
26
-
28
+
29
+ $effect(() => {
30
+ const element = elementRef;
31
+ if (!element) return;
32
+
33
+ const handleEvent = () => {
34
+ if (revealThreshold !== element.revealThreshold) {
35
+ revealThreshold = element.revealThreshold as any;
36
+ }
37
+ if (locale !== element.locale) {
38
+ locale = element.locale as any;
39
+ }
40
+ };
41
+
42
+ const events = ['change', 'input', 'scratch-progress', 'scratch-reveal'];
43
+ for (const event of events) {
44
+ element.addEventListener(event, handleEvent);
45
+ }
46
+ return () => {
47
+ for (const event of events) {
48
+ element.removeEventListener(event, handleEvent);
49
+ }
50
+ };
51
+ });
52
+
27
53
  </script>
28
54
 
29
- <uibit-scratch-reveal bind:this={elementRef} {...$$restProps}>
55
+ <uibit-scratch-reveal bind:this={elementRef} {...restProps}>
30
56
 
31
57
  {#if children}
32
58
  {@render children()}
@@ -1,28 +1,73 @@
1
- import { defineComponent, h } from 'vue';
1
+ import { defineComponent, h, ref, watch } from 'vue';
2
2
  import type { ScratchReveal as HTMLElementClass } from '@uibit/scratch-reveal';
3
3
  import '@uibit/scratch-reveal';
4
-
5
4
  export const ScratchReveal = defineComponent({
6
- name: 'ScratchReveal',
7
- props: {
8
- revealThreshold: { type: [String, Number, Boolean, Array, Object] as any },
9
- locale: { type: [String, Number, Boolean, Array, Object] as any }
10
- },
11
- emits: ['scratch-progress', 'scratch-reveal'],
12
- setup(props, { slots, emit }) {
13
- return () => {
14
- const eventListeners: Record<string, any> = {};
15
- eventListeners['onScratch-progress'] = (event: Event) => {
16
- emit('scratch-progress', event);
17
- };
18
- eventListeners['onScratch-reveal'] = (event: Event) => {
19
- emit('scratch-reveal', event);
20
- };
21
-
22
- return h('uibit-scratch-reveal', {
23
- ...props,
24
- ...eventListeners
25
- }, slots.default?.());
26
- };
27
- }
5
+ name: 'ScratchReveal',
6
+ props: {
7
+ revealThreshold: {
8
+ type: [
9
+ String,
10
+ Number,
11
+ Boolean,
12
+ Array,
13
+ Object
14
+ ] as any
15
+ },
16
+ locale: {
17
+ type: [
18
+ String,
19
+ Number,
20
+ Boolean,
21
+ Array,
22
+ Object
23
+ ] as any
24
+ }
25
+ },
26
+ emits: [
27
+ 'scratch-progress',
28
+ 'scratch-reveal'
29
+ ],
30
+ setup (props, { slots, emit }) {
31
+ const elementRef = ref<HTMLElementClass | null>(null);
32
+ if (false) {
33
+ watch(()=>props.modelValue, (newVal)=>{
34
+ if (elementRef.value && newVal !== undefined && elementRef.value.value !== newVal) {
35
+ elementRef.value.value = newVal;
36
+ }
37
+ });
38
+ watch(()=>props.value, (newVal)=>{
39
+ if (elementRef.value && newVal !== undefined && elementRef.value.value !== newVal) {
40
+ elementRef.value.value = newVal;
41
+ }
42
+ });
43
+ }
44
+ watch(()=>props.revealThreshold, (newVal)=>{
45
+ if (elementRef.value && newVal !== undefined) {
46
+ (elementRef.value as any).revealThreshold = newVal;
47
+ }
48
+ });
49
+ watch(()=>props.locale, (newVal)=>{
50
+ if (elementRef.value && newVal !== undefined) {
51
+ (elementRef.value as any).locale = newVal;
52
+ }
53
+ });
54
+ return ()=>{
55
+ const eventListeners: Record<string, any> = {};
56
+ eventListeners['onScratch-progress'] = (event: Event)=>{
57
+ emit('scratch-progress', event);
58
+ };
59
+ eventListeners['onScratch-reveal'] = (event: Event)=>{
60
+ emit('scratch-reveal', event);
61
+ };
62
+ const mergedProps = {
63
+ ...props,
64
+ ...eventListeners,
65
+ ref: elementRef
66
+ };
67
+ if (false && props.modelValue !== undefined) {
68
+ (mergedProps as any).value = props.modelValue;
69
+ }
70
+ return h('uibit-scratch-reveal', mergedProps, slots.default?.());
71
+ };
72
+ }
28
73
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uibit/scratch-reveal",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "A gamified, accessible scratch-off panel component that reveals hidden content. Users can scratch off the overlay canvas layer by dragging or touching.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "./scratch-reveal.css": "./dist/scratch-reveal.css",
14
14
  "./custom-elements.json": "./custom-elements.json",
15
- "./react": "./dist/frameworks/react/index.d.ts",
15
+ "./react": "./dist/frameworks/react/index.ts",
16
16
  "./vue": "./dist/frameworks/vue/index.ts",
17
17
  "./svelte": "./dist/frameworks/svelte/index.svelte",
18
18
  "./angular": "./dist/frameworks/angular/index.ts",
@@ -54,7 +54,7 @@
54
54
  "@types/node": "^20.19.43",
55
55
  "lit": "^3.3.3",
56
56
  "typescript": "7.0.2",
57
- "@uibit/codegen": "0.1.0"
57
+ "@uibit/codegen": "0.2.0"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "lit": "^3.0.0",
@@ -1,20 +0,0 @@
1
- import type { HTMLAttributes, ClassAttributes } from 'react';
2
- import type { ScratchReveal as HTMLElementClass } from '@uibit/scratch-reveal';
3
- import '@uibit/scratch-reveal';
4
-
5
- declare global {
6
- namespace React {
7
- namespace JSX {
8
- interface IntrinsicElements {
9
- 'uibit-scratch-reveal': ClassAttributes<HTMLElementClass> & HTMLAttributes<HTMLElementClass> & {
10
- children?: React.ReactNode;
11
- class?: string;
12
- revealThreshold?: number;
13
- locale?: string;
14
- onScratchProgress?: (event: any) => void;
15
- onScratchReveal?: (event: any) => void;
16
- };
17
- }
18
- }
19
- }
20
- }