@uibit/image-comparison 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,51 +1,54 @@
1
- import { Component, ChangeDetectionStrategy, ElementRef, input, effect, output, booleanAttribute, numberAttribute } from '@angular/core';
1
+ import { Component, ChangeDetectionStrategy, ElementRef, model, input, effect, output, booleanAttribute, numberAttribute } from '@angular/core';
2
2
  import '@uibit/image-comparison';
3
3
  import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
4
4
  import type { ComparisonMode } from '@uibit/image-comparison';
5
-
6
5
  @Component({
7
- selector: 'uibit-image-comparison',
8
- template: '<ng-content></ng-content>',
9
- changeDetection: ChangeDetectionStrategy.OnPush,
10
- standalone: true,
11
- host: {
12
- '(comparison-progress)': 'comparisonProgress.emit($event)'
13
- }
6
+ selector: 'uibit-image-comparison',
7
+ template: '<ng-content></ng-content>',
8
+ changeDetection: ChangeDetectionStrategy.OnPush,
9
+ standalone: true,
10
+ host: {
11
+ '(comparison-progress)': 'comparisonProgress.emit($event); progress.set($event.detail?.progress !== undefined ? $event.detail.progress : $event.target.progress)'
12
+ }
14
13
  })
15
14
  export class NgxImageComparison {
16
- constructor(private el: ElementRef<HTMLElementClass>) {
17
- effect(() => {
18
- if (this.el.nativeElement) {
19
- this.el.nativeElement.mode = this.mode();
20
- }
15
+ constructor(private el: ElementRef<HTMLElementClass>){
16
+ effect(()=>{
17
+ if (this.el.nativeElement) {
18
+ this.el.nativeElement.mode = this.mode();
19
+ }
20
+ });
21
+ effect(()=>{
22
+ if (this.el.nativeElement) {
23
+ this.el.nativeElement.progress = this.progress();
24
+ }
25
+ });
26
+ effect(()=>{
27
+ if (this.el.nativeElement) {
28
+ this.el.nativeElement.interactive = this.interactive();
29
+ }
30
+ });
31
+ effect(()=>{
32
+ if (this.el.nativeElement) {
33
+ this.el.nativeElement.hoverReveal = this.hoverReveal();
34
+ }
35
+ });
36
+ effect(()=>{
37
+ if (this.el.nativeElement) {
38
+ this.el.nativeElement.locale = this.locale();
39
+ }
40
+ });
41
+ }
42
+ readonly mode = input<ComparisonMode>('horizontal');
43
+ readonly progress = model<number>(50);
44
+ readonly interactive = input<boolean, any>(true, {
45
+ transform: booleanAttribute
21
46
  });
22
- effect(() => {
23
- if (this.el.nativeElement) {
24
- this.el.nativeElement.progress = this.progress();
25
- }
47
+ readonly hoverReveal = input<boolean, any>(false, {
48
+ transform: booleanAttribute
26
49
  });
27
- effect(() => {
28
- if (this.el.nativeElement) {
29
- this.el.nativeElement.interactive = this.interactive();
30
- }
31
- });
32
- effect(() => {
33
- if (this.el.nativeElement) {
34
- this.el.nativeElement.hoverReveal = this.hoverReveal();
35
- }
36
- });
37
- effect(() => {
38
- if (this.el.nativeElement) {
39
- this.el.nativeElement.locale = this.locale();
40
- }
41
- });
42
- }
43
-
44
- readonly mode = input<ComparisonMode, any>('horizontal');
45
- readonly progress = input<number, any>(50, { transform: numberAttribute });
46
- readonly interactive = input<boolean, any>(true, { transform: booleanAttribute });
47
- readonly hoverReveal = input<boolean, any>(false, { transform: booleanAttribute });
48
- readonly locale = input<string, any>('');
49
-
50
- readonly comparisonProgress = output<CustomEvent<progress: number>>();
50
+ readonly locale = input<string>('');
51
+ readonly comparisonProgress = output<CustomEvent<{
52
+ progress: number;
53
+ }>>();
51
54
  }
@@ -1,10 +1,18 @@
1
1
  import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
2
2
  import '@uibit/image-comparison';
3
+ import type { ComparisonMode } from '@uibit/image-comparison';
3
4
 
4
5
  declare global {
5
6
  namespace astroHTML.JSX {
6
7
  interface IntrinsicElements {
7
- 'uibit-image-comparison': Partial<HTMLElementClass> & astroHTML.JSX.HTMLAttributes;
8
+ 'uibit-image-comparison': Partial<HTMLElementClass> & astroHTML.JSX.HTMLAttributes & {
9
+ mode?: ComparisonMode;
10
+ progress?: number;
11
+ interactive?: boolean;
12
+ hoverReveal?: boolean;
13
+ locale?: string;
14
+ "on:comparison-progress"?: (event: CustomEvent<{ progress: number }>) => void;
15
+ };
8
16
  }
9
17
  }
10
18
  }
@@ -12,6 +12,8 @@ declare module 'preact' {
12
12
  interactive?: boolean;
13
13
  hoverReveal?: boolean;
14
14
  locale?: string;
15
+ onComparisonProgress?: (event: CustomEvent<{ progress: number }>) => void;
16
+ "on:comparison-progress"?: (event: CustomEvent<{ progress: number }>) => void;
15
17
  };
16
18
  }
17
19
  }
@@ -0,0 +1,115 @@
1
+ import React, { useRef, useEffect, useLayoutEffect, useImperativeHandle } from 'react';
2
+ import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
3
+ import '@uibit/image-comparison';
4
+ import type { ComparisonMode } from '@uibit/image-comparison';
5
+ const useTypeOfLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
6
+ export interface ImageComparisonProps extends Omit<React.HTMLAttributes<HTMLElementClass>, 'mode' | 'progress' | 'interactive' | 'hoverReveal' | 'locale' | 'onComparisonProgress'> {
7
+ children?: React.ReactNode;
8
+ mode?: ComparisonMode;
9
+ progress?: number;
10
+ interactive?: boolean;
11
+ hoverReveal?: boolean;
12
+ locale?: string;
13
+ onComparisonProgress?: (event: CustomEvent<{
14
+ progress: number;
15
+ }>) => void;
16
+ }
17
+ export const ImageComparison = ({ ref, children, ...props }: ImageComparisonProps & {
18
+ ref?: React.Ref<HTMLElementClass>;
19
+ })=>{
20
+ const innerRef = useRef<HTMLElementClass>(null);
21
+ const propsRef = useRef(props);
22
+ propsRef.current = props;
23
+ useImperativeHandle(ref, ()=>innerRef.current!);
24
+ useTypeOfLayoutEffect(()=>{
25
+ const element = innerRef.current;
26
+ if (element && props.mode !== undefined) {
27
+ (element as any).mode = props.mode;
28
+ }
29
+ }, [
30
+ props.mode
31
+ ]);
32
+ useTypeOfLayoutEffect(()=>{
33
+ const element = innerRef.current;
34
+ if (element && props.progress !== undefined) {
35
+ (element as any).progress = props.progress;
36
+ }
37
+ }, [
38
+ props.progress
39
+ ]);
40
+ useTypeOfLayoutEffect(()=>{
41
+ const element = innerRef.current;
42
+ if (element && props.interactive !== undefined) {
43
+ (element as any).interactive = props.interactive;
44
+ }
45
+ }, [
46
+ props.interactive
47
+ ]);
48
+ useTypeOfLayoutEffect(()=>{
49
+ const element = innerRef.current;
50
+ if (element && props.hoverReveal !== undefined) {
51
+ (element as any).hoverReveal = props.hoverReveal;
52
+ }
53
+ }, [
54
+ props.hoverReveal
55
+ ]);
56
+ useTypeOfLayoutEffect(()=>{
57
+ const element = innerRef.current;
58
+ if (element && props.locale !== undefined) {
59
+ (element as any).locale = props.locale;
60
+ }
61
+ }, [
62
+ props.locale
63
+ ]);
64
+ useTypeOfLayoutEffect(()=>{
65
+ const element = innerRef.current;
66
+ if (!element) return;
67
+ const handleEvent = (event: Event)=>{
68
+ if (propsRef.current.onComparisonProgress) {
69
+ propsRef.current.onComparisonProgress(event as any);
70
+ }
71
+ };
72
+ element.addEventListener('comparison-progress', handleEvent);
73
+ return ()=>{
74
+ element.removeEventListener('comparison-progress', handleEvent);
75
+ };
76
+ }, []);
77
+ const domProps = {
78
+ ...props
79
+ };
80
+ const customPropNames = [
81
+ 'mode',
82
+ 'progress',
83
+ 'interactive',
84
+ 'hoverReveal',
85
+ 'locale',
86
+ 'onComparisonProgress'
87
+ ];
88
+ for (const key of customPropNames){
89
+ delete (domProps as any)[key];
90
+ }
91
+ return React.createElement('uibit-image-comparison', {
92
+ ref: innerRef,
93
+ ...domProps
94
+ }, children);
95
+ };
96
+ declare global {
97
+ namespace React {
98
+ namespace JSX {
99
+ interface IntrinsicElements {
100
+ 'uibit-image-comparison': React.ClassAttributes<HTMLElementClass> & React.HTMLAttributes<HTMLElementClass> & {
101
+ children?: React.ReactNode;
102
+ class?: string;
103
+ mode?: ComparisonMode;
104
+ progress?: number;
105
+ interactive?: boolean;
106
+ hoverReveal?: boolean;
107
+ locale?: string;
108
+ onComparisonProgress?: (event: CustomEvent<{
109
+ progress: number;
110
+ }>) => void;
111
+ };
112
+ }
113
+ }
114
+ }
115
+ }
@@ -12,7 +12,7 @@ declare module 'solid-js' {
12
12
  interactive?: boolean;
13
13
  hoverReveal?: boolean;
14
14
  locale?: string;
15
- "on:comparison-progress"?: (event: CustomEvent) => void;
15
+ "on:comparison-progress"?: (event: CustomEvent<{ progress: number }>) => void;
16
16
  };
17
17
  }
18
18
  }
@@ -11,6 +11,8 @@ declare module '@stencil/core' {
11
11
  interactive?: boolean;
12
12
  hoverReveal?: boolean;
13
13
  locale?: string;
14
+ onComparisonProgress?: (event: CustomEvent<{ progress: number }>) => void;
15
+ "on:comparison-progress"?: (event: CustomEvent<{ progress: number }>) => void;
14
16
  };
15
17
  }
16
18
  }
@@ -2,47 +2,82 @@
2
2
  import '@uibit/image-comparison';
3
3
  import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
4
4
  import type { ComparisonMode } from '@uibit/image-comparison';
5
-
6
- let {
7
- mode = undefined,
8
- progress = undefined,
9
- interactive = undefined,
10
- hoverReveal = undefined,
11
- locale = undefined,
12
- children
5
+
6
+ let {
7
+ mode = $bindable(),
8
+ progress = $bindable(),
9
+ interactive = $bindable(),
10
+ hoverReveal = $bindable(),
11
+ locale = $bindable(),
12
+ children,
13
+ ...restProps
13
14
  } = $props<{
14
- children?: any;
15
+ children?: import('svelte').Snippet;
15
16
  mode?: ComparisonMode;
16
17
  progress?: number;
17
18
  interactive?: boolean;
18
19
  hoverReveal?: boolean;
19
20
  locale?: string;
20
-
21
+
22
+ [key: string]: any;
21
23
  }>();
22
-
23
- let elementRef: HTMLElementClass | null = $state(null);
24
-
25
- $effect(() => {
26
- if (elementRef && mode !== undefined) {
24
+
25
+ let elementRef: HTMLElementClass | null = $state(null);
26
+
27
+ $effect(() => {
28
+ if (elementRef && mode !== undefined && elementRef.mode !== mode) {
27
29
  elementRef.mode = mode;
28
30
  }
29
- if (elementRef && progress !== undefined) {
31
+ if (elementRef && progress !== undefined && elementRef.progress !== progress) {
30
32
  elementRef.progress = progress;
31
33
  }
32
- if (elementRef && interactive !== undefined) {
34
+ if (elementRef && interactive !== undefined && elementRef.interactive !== interactive) {
33
35
  elementRef.interactive = interactive;
34
36
  }
35
- if (elementRef && hoverReveal !== undefined) {
37
+ if (elementRef && hoverReveal !== undefined && elementRef.hoverReveal !== hoverReveal) {
36
38
  elementRef.hoverReveal = hoverReveal;
37
39
  }
38
- if (elementRef && locale !== undefined) {
40
+ if (elementRef && locale !== undefined && elementRef.locale !== locale) {
39
41
  elementRef.locale = locale;
40
42
  }
41
43
  });
42
-
44
+
45
+ $effect(() => {
46
+ const element = elementRef;
47
+ if (!element) return;
48
+
49
+ const handleEvent = () => {
50
+ if (mode !== element.mode) {
51
+ mode = element.mode as any;
52
+ }
53
+ if (progress !== element.progress) {
54
+ progress = element.progress as any;
55
+ }
56
+ if (interactive !== element.interactive) {
57
+ interactive = element.interactive as any;
58
+ }
59
+ if (hoverReveal !== element.hoverReveal) {
60
+ hoverReveal = element.hoverReveal as any;
61
+ }
62
+ if (locale !== element.locale) {
63
+ locale = element.locale as any;
64
+ }
65
+ };
66
+
67
+ const events = ['change', 'input', 'comparison-progress'];
68
+ for (const event of events) {
69
+ element.addEventListener(event, handleEvent);
70
+ }
71
+ return () => {
72
+ for (const event of events) {
73
+ element.removeEventListener(event, handleEvent);
74
+ }
75
+ };
76
+ });
77
+
43
78
  </script>
44
79
 
45
- <uibit-image-comparison bind:this={elementRef} {...$$restProps}>
80
+ <uibit-image-comparison bind:this={elementRef} {...restProps}>
46
81
 
47
82
  {#if children}
48
83
  {@render children()}
@@ -1,29 +1,112 @@
1
- import { defineComponent, h } from 'vue';
1
+ import { defineComponent, h, ref, watch } from 'vue';
2
2
  import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
3
3
  import '@uibit/image-comparison';
4
4
  import type { ComparisonMode } from '@uibit/image-comparison';
5
-
6
5
  export const ImageComparison = defineComponent({
7
- name: 'ImageComparison',
8
- props: {
9
- mode: { type: [String, Number, Boolean, Array, Object] as any },
10
- progress: { type: [String, Number, Boolean, Array, Object] as any },
11
- interactive: { type: [String, Number, Boolean, Array, Object] as any },
12
- hoverReveal: { type: [String, Number, Boolean, Array, Object] as any },
13
- locale: { type: [String, Number, Boolean, Array, Object] as any }
14
- },
15
- emits: ['comparison-progress'],
16
- setup(props, { slots, emit }) {
17
- return () => {
18
- const eventListeners: Record<string, any> = {};
19
- eventListeners['onComparison-progress'] = (event: Event) => {
20
- emit('comparison-progress', event);
21
- };
22
-
23
- return h('uibit-image-comparison', {
24
- ...props,
25
- ...eventListeners
26
- }, slots.default?.());
27
- };
28
- }
6
+ name: 'ImageComparison',
7
+ props: {
8
+ mode: {
9
+ type: [
10
+ String,
11
+ Number,
12
+ Boolean,
13
+ Array,
14
+ Object
15
+ ] as any
16
+ },
17
+ progress: {
18
+ type: [
19
+ String,
20
+ Number,
21
+ Boolean,
22
+ Array,
23
+ Object
24
+ ] as any
25
+ },
26
+ interactive: {
27
+ type: [
28
+ String,
29
+ Number,
30
+ Boolean,
31
+ Array,
32
+ Object
33
+ ] as any
34
+ },
35
+ hoverReveal: {
36
+ type: [
37
+ String,
38
+ Number,
39
+ Boolean,
40
+ Array,
41
+ Object
42
+ ] as any
43
+ },
44
+ locale: {
45
+ type: [
46
+ String,
47
+ Number,
48
+ Boolean,
49
+ Array,
50
+ Object
51
+ ] as any
52
+ }
53
+ },
54
+ emits: [
55
+ 'comparison-progress'
56
+ ],
57
+ setup (props, { slots, emit }) {
58
+ const elementRef = ref<HTMLElementClass | null>(null);
59
+ if (false) {
60
+ watch(()=>props.modelValue, (newVal)=>{
61
+ if (elementRef.value && newVal !== undefined && elementRef.value.value !== newVal) {
62
+ elementRef.value.value = newVal;
63
+ }
64
+ });
65
+ watch(()=>props.value, (newVal)=>{
66
+ if (elementRef.value && newVal !== undefined && elementRef.value.value !== newVal) {
67
+ elementRef.value.value = newVal;
68
+ }
69
+ });
70
+ }
71
+ watch(()=>props.mode, (newVal)=>{
72
+ if (elementRef.value && newVal !== undefined) {
73
+ (elementRef.value as any).mode = newVal;
74
+ }
75
+ });
76
+ watch(()=>props.progress, (newVal)=>{
77
+ if (elementRef.value && newVal !== undefined) {
78
+ (elementRef.value as any).progress = newVal;
79
+ }
80
+ });
81
+ watch(()=>props.interactive, (newVal)=>{
82
+ if (elementRef.value && newVal !== undefined) {
83
+ (elementRef.value as any).interactive = newVal;
84
+ }
85
+ });
86
+ watch(()=>props.hoverReveal, (newVal)=>{
87
+ if (elementRef.value && newVal !== undefined) {
88
+ (elementRef.value as any).hoverReveal = newVal;
89
+ }
90
+ });
91
+ watch(()=>props.locale, (newVal)=>{
92
+ if (elementRef.value && newVal !== undefined) {
93
+ (elementRef.value as any).locale = newVal;
94
+ }
95
+ });
96
+ return ()=>{
97
+ const eventListeners: Record<string, any> = {};
98
+ eventListeners['onComparison-progress'] = (event: Event)=>{
99
+ emit('comparison-progress', event);
100
+ };
101
+ const mergedProps = {
102
+ ...props,
103
+ ...eventListeners,
104
+ ref: elementRef
105
+ };
106
+ if (false && props.modelValue !== undefined) {
107
+ (mergedProps as any).value = props.modelValue;
108
+ }
109
+ return h('uibit-image-comparison', mergedProps, slots.default?.());
110
+ };
111
+ }
29
112
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uibit/image-comparison",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Displays overlapping before/after content layers with interactive diagonal, radial, horizontal, or vertical splits.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "./image-comparison.css": "./dist/image-comparison.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,23 +0,0 @@
1
- import type { HTMLAttributes, ClassAttributes } from 'react';
2
- import type { ImageComparison as HTMLElementClass } from '@uibit/image-comparison';
3
- import '@uibit/image-comparison';
4
- import type { ComparisonMode } from '@uibit/image-comparison';
5
-
6
- declare global {
7
- namespace React {
8
- namespace JSX {
9
- interface IntrinsicElements {
10
- 'uibit-image-comparison': ClassAttributes<HTMLElementClass> & HTMLAttributes<HTMLElementClass> & {
11
- children?: React.ReactNode;
12
- class?: string;
13
- mode?: ComparisonMode;
14
- progress?: number;
15
- interactive?: boolean;
16
- hoverReveal?: boolean;
17
- locale?: string;
18
- onComparisonProgress?: (event: any) => void;
19
- };
20
- }
21
- }
22
- }
23
- }