Skip to content

Commit 88794dc

Browse files
committed
chore: Port code to TypeScript
1 parent 340f2a9 commit 88794dc

File tree

8 files changed

+954
-110
lines changed

8 files changed

+954
-110
lines changed

package-lock.json

Lines changed: 822 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
"action",
1212
"javascript"
1313
],
14-
"repository": "https://github.com/untemps/svelte-use-tooltip.git",
15-
"bugs": "https://github.com/untemps/svelte-use-tooltip/issues",
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/untemps/svelte-use-tooltip.git"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/untemps/svelte-use-tooltip/issues"
20+
},
1621
"publishConfig": {
1722
"access": "public"
1823
},
@@ -26,6 +31,7 @@
2631
],
2732
"exports": {
2833
".": {
34+
"types": "./dist/types/index.d.ts",
2935
"svelte": "./dist/index.js"
3036
}
3137
},
@@ -44,6 +50,7 @@
4450
"@sveltejs/vite-plugin-svelte": "^4.0.0",
4551
"@testing-library/jest-dom": "^6.6.3",
4652
"@testing-library/svelte": "^5.2.6",
53+
"@types/node": "^22.10.1",
4754
"@vitest/coverage-v8": "^2.1.6",
4855
"eslint": "^9.7.0",
4956
"eslint-config-prettier": "^9.1.0",
@@ -55,8 +62,9 @@
5562
"publint": "^0.2.0",
5663
"semantic-release": "^24.2.0",
5764
"svelte": "^5.0.0",
58-
"typescript": "^5.3.2",
65+
"typescript": "^5.7.2",
5966
"vite": "^5.0.11",
67+
"vite-plugin-dts": "^4.3.0",
6068
"vitest": "^2.0.4"
6169
},
6270
"dependencies": {

src/lib/Tooltip.js renamed to src/lib/Tooltip.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
import { DOMObserver } from '@untemps/dom-observer';
22
import { standby } from '@untemps/utils/async/standby';
33

4+
interface ContentAction {
5+
eventType: string;
6+
callback?: (...args: any[]) => void;
7+
callbackParams?: any[];
8+
closeOnCallback?: boolean;
9+
}
10+
11+
interface ContentActions {
12+
[key: string]: ContentAction;
13+
}
14+
15+
interface EventListener {
16+
trigger: HTMLElement;
17+
eventType: string;
18+
listener: (event: Event) => void;
19+
}
20+
421
class Tooltip {
5-
static #instances = [];
6-
7-
#tooltip = null;
8-
9-
#target = null;
10-
#content = null;
11-
#contentSelector = null;
12-
#contentActions = null;
13-
#containerClassName = null;
14-
#position = null;
15-
#animated = false;
16-
#animationEnterClassName = null;
17-
#animationLeaveClassName = null;
18-
#enterDelay = 0;
19-
#leaveDelay = 0;
20-
#onEnter = null;
21-
#onLeave = null;
22-
#offset = 10;
23-
24-
#observer = null;
25-
#events = [];
26-
#delay = null;
27-
#transitioning = false;
28-
29-
#boundEnterHandler = null;
30-
#boundLeaveHandler = null;
31-
#boundWindowChangeHandler = null;
22+
static #instances: Tooltip[] = [];
23+
24+
#tooltip: HTMLDivElement | null = null;
25+
#target: HTMLElement;
26+
#content: string | null;
27+
#contentSelector: string | null;
28+
#contentActions: ContentActions | null;
29+
#containerClassName: string | null;
30+
#position: 'top' | 'bottom' | 'left' | 'right';
31+
#animated: boolean;
32+
#animationEnterClassName: string;
33+
#animationLeaveClassName: string;
34+
#enterDelay: number;
35+
#leaveDelay: number;
36+
#onEnter: (() => void) | null;
37+
#onLeave: (() => void) | null;
38+
#offset: number;
39+
40+
#observer: typeof DOMObserver | null;
41+
#events: EventListener[] = [];
42+
#delay: NodeJS.Timeout | null = null;
43+
#transitioning: boolean = false;
44+
45+
#boundEnterHandler: ((e: Event) => void) | null = null;
46+
#boundLeaveHandler: ((e: Event) => void) | null = null;
47+
#boundWindowChangeHandler: ((e: Event) => void) | null = null;
3248

3349
static destroy() {
3450
Tooltip.#instances.forEach((instance) => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as useTooltip } from './useTooltip';
2+
export type { TooltipOptions } from './types';

src/lib/types/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export interface TooltipOptions {
2+
content?: string;
3+
contentSelector?: string;
4+
contentActions?: {
5+
[key: string]: {
6+
eventType: string;
7+
callback?: (...args: any[]) => void;
8+
callbackParams?: any[];
9+
closeOnCallback?: boolean;
10+
};
11+
};
12+
containerClassName?: string;
13+
position?: 'top' | 'bottom' | 'left' | 'right';
14+
animated?: boolean;
15+
animationEnterClassName?: string;
16+
animationLeaveClassName?: string;
17+
enterDelay?: number;
18+
leaveDelay?: number;
19+
onEnter?: () => void;
20+
onLeave?: () => void;
21+
offset?: number;
22+
disabled?: boolean;
23+
}

src/lib/useTooltip.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/lib/useTooltip.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Tooltip from './Tooltip';
2+
import type { TooltipOptions } from './types';
3+
4+
import './useTooltip.css';
5+
6+
const useTooltip = (node: HTMLElement, options: TooltipOptions) => {
7+
const tooltip = new Tooltip(
8+
node,
9+
options.content,
10+
options.contentSelector,
11+
options.contentActions,
12+
options.containerClassName,
13+
options.position,
14+
options.animated,
15+
options.animationEnterClassName,
16+
options.animationLeaveClassName,
17+
options.enterDelay,
18+
options.leaveDelay,
19+
options.onEnter,
20+
options.onLeave,
21+
options.offset,
22+
options.disabled
23+
);
24+
25+
return {
26+
update: (newOptions: TooltipOptions) =>
27+
tooltip.update(
28+
newOptions.content,
29+
newOptions.contentSelector,
30+
newOptions.contentActions,
31+
newOptions.containerClassName,
32+
newOptions.position,
33+
newOptions.animated,
34+
newOptions.animationEnterClassName,
35+
newOptions.animationLeaveClassName,
36+
newOptions.enterDelay,
37+
newOptions.leaveDelay,
38+
newOptions.onEnter,
39+
newOptions.onLeave,
40+
newOptions.offset,
41+
newOptions.disabled
42+
),
43+
destroy: () => tooltip.destroy()
44+
};
45+
};
46+
47+
export default useTooltip;

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"declarationDir": "./dist/types"
6+
}
7+
}

0 commit comments

Comments
 (0)