Skip to content

Commit e6a854d

Browse files
authored
Merge pull request #10 from Namchee/unified-options
feat: Add unified options
2 parents 1fc2868 + 473db19 commit e6a854d

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Prop Name | Type | Default | Description
9393
`hideOnOut` | `boolean` | `false` | Determines if the custom cursor should be hidden when the cursor leaves the viewport
9494
`contentPosition` | `string` | `center` | Determines the position of `contents` slot relative to the cursor. Possible values are `center`, `bottom`, `right`
9595
`lerp` | `number` | `1` | [Linear Interpolation Value](https://codepen.io/ma77os/pen/KGIEh)
96+
`options` | `Object` | `{}` | All of other options in one single object. Will gracefully fallback if some values are not provided.
9697

9798
## Slots
9899

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "@namchee/tetikus",
3-
"version": "0.3.1",
3+
"version": "0.4.2",
44
"description": "A custom cursor component for Vue 3 ✌️",
55
"author": "Cristopher Namchee <[email protected]>",
66
"scripts": {
77
"lint": "eslint",
88
"build": "rm -rf dist/ && rollup -c",
99
"build:watch": "npm run build -- --watch",
10-
"build:verbose": "npm run build -- --verbose",
1110
"test": "ava",
1211
"postuninstall": "npm prune"
1312
},

src/common/types.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1+
// Exception level to be thrown.
2+
// Internal usage
13
export enum ExceptionLevel {
24
WARNING = 1,
35
ERROR = 2,
46
}
57

8+
// Options to be passed on `app.use()` calls
69
export interface ConstructorOptions {
710
directiveName?: string;
811
transitionSpeed?: number;
912
easing?: string;
1013
delay?: number;
1114
}
1215

16+
// Options to be passed on props change
17+
// Example: color: { value: black }, then tetikus background
18+
// will be changed to black
1319
export interface TransformOpts<T> {
1420
value: T;
1521
duration?: number;
1622
delay?: number;
1723
easing?: string;
1824
}
1925

26+
// Options to be passed on click or hover events.
2027
export interface TransformProps {
2128
id?: string | string[];
2229
scale?: TransformOpts<number> | number;
@@ -26,16 +33,41 @@ export interface TransformProps {
2633
opacity?: TransformOpts<number> | number;
2734
}
2835

36+
// Internal usage
2937
export interface CSSStyles {
3038
[key: string]: string | number;
3139
}
3240

41+
// Internal usage
3342
export interface CSSAnimation {
3443
cssStyles: CSSStyles;
3544
transitionString: string;
3645
}
3746

47+
// Expected behavior from domElement on hover interaction
3848
export interface HoverBehavior {
3949
domElement: HTMLElement;
4050
transformProps: TransformProps;
4151
}
52+
53+
// Unified tetikus props
54+
// Useful to avoid apropcalypse
55+
export interface TetikusProps {
56+
showDefaultCursor?: boolean;
57+
throttleSpeed?: number;
58+
borderWidth?: number;
59+
color?: string;
60+
borderColor?: string;
61+
size?: number;
62+
inverColor?: boolean;
63+
buttonMap?: string[];
64+
clickBehavior?: TransformProps;
65+
showOnTouch?: boolean;
66+
opacity?: number;
67+
hideOnOut?: boolean;
68+
contentPosition?: string;
69+
lerp?: number;
70+
defaultTransitionSpeed?: number;
71+
defaultEasing?: string;
72+
defaultDelay?: number;
73+
}

src/components/Tetikus/Tetikus.vue

+32-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { throttle } from './../../util/throttle';
1414
import { lerp } from './../../util/math';
1515
import { generateCSSTransform } from './../../util/dom';
1616
import { hoverState } from './../../directives/hover';
17-
import { TransformProps, HoverBehavior } from './../../common/types';
17+
import {
18+
TransformProps,
19+
HoverBehavior,
20+
TetikusProps,
21+
} from './../../common/types';
1822
import {
1923
defaultTransitionSpeed,
2024
defaultEasingFunction,
@@ -79,8 +83,10 @@ export default defineComponent({
7983
buttonMap: {
8084
type: Array,
8185
default: () => ['left'],
82-
validator: (val: Array<string>) => {
83-
return val.every((v) => ['left', 'middle', 'right'].includes(v));
86+
validator: (values: string[]) => {
87+
return values.every(
88+
(value: string) => ['left', 'middle', 'right'].includes(value),
89+
);
8490
},
8591
},
8692
@@ -145,9 +151,20 @@ export default defineComponent({
145151
type: Number,
146152
default: defaultDelay.value,
147153
},
154+
155+
// Unified tetikus props
156+
// Useful to avoid apropcalypse
157+
options: {
158+
type: Object as () => TetikusProps,
159+
default: {},
160+
},
148161
},
149162
150-
setup(props, { slots, emit }) {
163+
setup(properties, { slots, emit }) {
164+
// construct new props unified object from options
165+
// any undefined values will fallback normally.
166+
const props = { ...properties, ...properties.options };
167+
151168
// wrapper element ref
152169
const wrapper: Ref<HTMLElement | null> = ref(null);
153170
// cursor element ref
@@ -164,7 +181,7 @@ export default defineComponent({
164181
165182
// css styles for cursor wrapper element
166183
const wrapperStyle = computed(() => {
167-
const baseStyles: Record<string, unknown> = {
184+
const baseStyles: Record<string, string | number> = {
168185
'opacity': props.opacity,
169186
'mix-blend-mode': props.invertColor ? 'difference' : 'normal',
170187
'flex-direction': props.contentPosition === 'bottom' ?
@@ -199,7 +216,8 @@ export default defineComponent({
199216
const contentStyle = computed(() => {
200217
return {
201218
'position': props.contentPosition === 'center' ?
202-
'absolute' : 'center',
219+
'absolute' :
220+
'center',
203221
};
204222
});
205223
@@ -309,7 +327,9 @@ export default defineComponent({
309327
310328
// apply transformation on mouse button press
311329
const handleMouseDown = (event: MouseEvent): void => {
312-
if (!props.buttonMap.includes(buttonMap.get(event.button))) {
330+
const button = buttonMap.get(event.button) as string;
331+
332+
if (!props.buttonMap.includes(button)) {
313333
return;
314334
}
315335
@@ -334,7 +354,9 @@ export default defineComponent({
334354
335355
// apply another transform on cursor when mouse button is lifted
336356
const handleMouseUp = (event: MouseEvent): void => {
337-
if (!props.buttonMap.includes(buttonMap.get(event.button))) {
357+
const button = buttonMap.get(event.button) as string;
358+
359+
if (!props.buttonMap.includes(button)) {
338360
return;
339361
}
340362
@@ -384,6 +406,8 @@ export default defineComponent({
384406
...defaultTransformStyle.value,
385407
...transformProps,
386408
};
409+
// prevent TetikusWarning error
410+
delete transformTarget.id;
387411
388412
applyTransform(defaultTransformStyle.value, transformTarget, false);
389413
}

0 commit comments

Comments
 (0)