Skip to content

Commit

Permalink
HintTooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
binrysearch committed Sep 1, 2024
1 parent 3a46f5c commit c58d62e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
37 changes: 37 additions & 0 deletions src/packages/hint/hintTooltip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Tooltip, TooltipProps } from "../tooltip/tooltip";
import van from "../dom/van";

const { a } = van.tags;

export type HintTooltipProps = Omit<TooltipProps, "hintMode"> & {
closeButtonEnabled: boolean;
closeButtonOnClick: () => void;
closeButtonLabel: string;
closeButtonClassName: string;
};

export const HintTooltip = ({
closeButtonEnabled,
closeButtonOnClick,
closeButtonLabel,
closeButtonClassName,
...props
}: HintTooltipProps) => {
return Tooltip(
{
...props,
hintMode: true,
},
[
closeButtonEnabled ?
a(
{
className: closeButtonClassName,
role: "button",
onclick: closeButtonOnClick,
},
closeButtonLabel
) : null,
]
);
};
5 changes: 2 additions & 3 deletions src/packages/hint/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { setClass } from "../../util/className";
import { hideHint } from "./hide";
import { setPositionRelativeTo } from "../../util/setPositionRelativeTo";
import DOMEvent from "../../util/DOMEvent";
import { Tooltip } from "../tooltip/tooltip";
import getOffset from "../../util/getOffset";
import { HintTooltip } from "./hintTooltip";

// The hint close function used when the user clicks outside the hint
let _hintCloseFunction: () => void | undefined;
Expand Down Expand Up @@ -89,13 +89,12 @@ export async function showHintDialog(hint: Hint, stepId: number) {

if (!hintItem) return;

const tooltipLayer = Tooltip({
const tooltipLayer = HintTooltip({
position: hintItem.position,
text: item.hint || "",
targetOffset: getOffset(hintItem.element as HTMLElement),
// hints don't have step numbers
showStepNumbers: false,
hintMode: true,

autoPosition: hint.getOption("autoPosition"),
positionPrecedence: hint.getOption("positionPrecedence"),
Expand Down
54 changes: 18 additions & 36 deletions src/packages/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import getOffset, { Offset } from "../../util/getOffset";
import getWindowSize from "../../util/getWindowSize";
import van, { State } from "../dom/van";
import van, { ChildDom, State } from "../dom/van";
import {
arrowClassName,
tooltipClassName,
tooltipTextClassName,
} from "../tour/classNames";
import { determineAutoPosition, TooltipPosition } from "./tooltipPosition";

const { div, p, a } = van.tags;
const { div, p } = van.tags;

export const TooltipArrow = (props: {
tooltipPosition: State<TooltipPosition>;
Expand Down Expand Up @@ -300,7 +300,7 @@ const alignTooltip = (
}
};

type TooltipProps = {
export type TooltipProps = {
position: TooltipPosition;
text: string;
targetOffset: Offset;
Expand All @@ -310,29 +310,22 @@ type TooltipProps = {
// auto-alignment properties
autoPosition: boolean;
positionPrecedence: TooltipPosition[];

closeButtonEnabled: boolean;
closeButtonOnClick: () => void;
closeButtonLabel: string;
closeButtonClassName: string;
};

export const Tooltip = ({
position: initialPosition,
text,
targetOffset,
hintMode = false,
showStepNumbers = false,

// auto-alignment properties
autoPosition = true,
positionPrecedence = [],

closeButtonEnabled,
closeButtonOnClick,
closeButtonLabel,
closeButtonClassName,
}: TooltipProps) => {
export const Tooltip = (
{
position: initialPosition,
text,
targetOffset,
hintMode = false,
showStepNumbers = false,

// auto-alignment properties
autoPosition = true,
positionPrecedence = [],
}: TooltipProps,
children?: ChildDom[]
) => {
const position = van.state<TooltipPosition>(initialPosition);
const top = van.state<string>("auto");
const right = van.state<string>("auto");
Expand Down Expand Up @@ -404,18 +397,7 @@ export const Tooltip = ({
tooltipPosition: position,
tooltipBottomOverflow: tooltipBottomOverflow,
}),
div({ className: tooltipTextClassName }, [
p(text),
closeButtonEnabled ??
a(
{
className: closeButtonClassName,
role: "button",
onclick: closeButtonOnClick,
},
closeButtonLabel
),
]),
div({ className: tooltipTextClassName }, [p(text), children]),
]
);

Expand Down

0 comments on commit c58d62e

Please sign in to comment.