From 5f66a15635e4f17687fed0d6f5ee4d8e328fedb2 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 31 Dec 2022 18:52:06 -0500 Subject: [PATCH] Update --- packages/d3tooltip/src/index.ts | 49 ++++++++++++--------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/d3tooltip/src/index.ts b/packages/d3tooltip/src/index.ts index 9c5c9fe5..931d92c3 100644 --- a/packages/d3tooltip/src/index.ts +++ b/packages/d3tooltip/src/index.ts @@ -3,22 +3,17 @@ import type { BaseType, ContainerElement, Selection } from 'd3'; import { is } from 'ramda'; import functor from './utils/functor'; -interface Options< - GElement extends ContainerElement, - Datum, - PElement extends BaseType, - PDatum -> { +interface Options { left: number | undefined; top: number | undefined; offset: { left: number; top: number; }; - root: Selection | undefined; + root: Selection | undefined; } -const defaultOptions: Options = { +const defaultOptions: Options = { left: undefined, // mouseX top: undefined, // mouseY offset: { left: 0, top: 0 }, @@ -27,14 +22,12 @@ const defaultOptions: Options = { export type StyleValue = string | number | boolean; -interface Tip< - GElement extends ContainerElement, - Datum, - PElement extends BaseType, - PDatum -> { - (selection: Selection): void; - styles: (this: this, value: { [key: string]: StyleValue }) => this; +interface Tip { + (selection: Selection): void; + styles: ( + this: this, + value: { [key: string]: StyleValue } | undefined + ) => this; text: ( this: this, value: @@ -43,19 +36,11 @@ interface Tip< ) => this; } -export function tooltip< - GElement extends ContainerElement, - Datum, - PElement extends BaseType, - PDatum ->( +export function tooltip( className = 'tooltip', - options: Partial> = {} -): Tip { - const { left, top, offset, root } = { - ...defaultOptions, - ...options, - } as Options; + options: Partial = {} +): Tip { + const { left, top, offset, root } = { ...defaultOptions, ...options }; let text: ( datum: Datum, @@ -64,12 +49,12 @@ export function tooltip< ) => string = () => ''; let styles: { [key: string]: StyleValue } = {}; - let el: Selection; - const anchor: Selection = + let el: Selection; + const anchor: Selection = root || d3.select('body'); const rootNode = anchor.node()!; - function tip(selection: Selection) { + function tip(selection: Selection) { selection.on('mouseover.tip', (node) => { const [mouseX, mouseY] = d3.mouse(rootNode); const [x, y] = [left || mouseX + offset.left, top || mouseY - offset.top]; @@ -105,7 +90,7 @@ export function tooltip< tip.styles = function setStyles( this: typeof tip, - value: { [key: string]: StyleValue } + value: { [key: string]: StyleValue } | undefined ) { if (is(Object, value)) { styles = { ...styles, ...value };