-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edc92ca
commit 9c6b72b
Showing
8 changed files
with
311 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import isFixed from "../../../util/isFixed"; | ||
import van from "../../dom/van"; | ||
import { | ||
fixedHintClassName, | ||
hintClassName, | ||
hintDotClassName, | ||
hintNoAnimationClassName, | ||
hintPulseClassName, | ||
} from "../className"; | ||
import { HintItem, HintPosition } from "../hintItem"; | ||
import { dataStepAttribute } from "../dataAttributes"; | ||
import { alignHintPosition } from "../position"; | ||
|
||
const { a, div } = van.tags; | ||
|
||
export type HintProps = { | ||
index: number; | ||
hintItem: HintItem; | ||
onClick: (e: any) => void; | ||
}; | ||
|
||
const className = (hintItem: HintItem) => { | ||
const classNames = [hintClassName]; | ||
|
||
if (!hintItem.hintAnimation) { | ||
classNames.push(hintNoAnimationClassName); | ||
} | ||
|
||
if (isFixed(hintItem.element as HTMLElement)) { | ||
classNames.push(fixedHintClassName); | ||
} | ||
|
||
return classNames.join(" "); | ||
}; | ||
|
||
const HintDot = () => div({ className: hintDotClassName }); | ||
const HintPulse = () => div({ className: hintPulseClassName }); | ||
|
||
export const HintIcon = ({ index, hintItem, onClick }: HintProps) => { | ||
const hintElement = a( | ||
{ | ||
[dataStepAttribute]: index.toString(), | ||
className: className(hintItem), | ||
role: "button", | ||
tabindex: 0, | ||
onclick: onClick, | ||
}, | ||
HintDot(), | ||
HintPulse() | ||
); | ||
|
||
alignHintPosition( | ||
hintItem.hintPosition as HintPosition, | ||
hintElement, | ||
hintItem.element as HTMLElement | ||
); | ||
|
||
return hintElement; | ||
}; |
7 changes: 4 additions & 3 deletions
7
src/packages/hint/hintTooltip.ts → src/packages/hint/components/HintTooltip.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import van from "../../dom/van"; | ||
import { hintsClassName } from "../className"; | ||
import { hideHint } from "../hide"; | ||
import { Hint } from "../hint"; | ||
import { showHintDialog } from "../tooltip"; | ||
import { HintIcon } from "./HintIcon"; | ||
import { ReferenceLayer } from "./ReferenceLayer"; | ||
|
||
const { div } = van.tags; | ||
|
||
export type HintsRootProps = { | ||
hint: Hint; | ||
}; | ||
|
||
/** | ||
* Returns an event handler unique to the hint iteration | ||
*/ | ||
const getHintClick = (hint: Hint, i: number) => (e: Event) => { | ||
const evt = e ? e : window.event; | ||
|
||
if (evt && evt.stopPropagation) { | ||
evt.stopPropagation(); | ||
} | ||
|
||
if (evt && evt.cancelBubble !== null) { | ||
evt.cancelBubble = true; | ||
} | ||
|
||
showHintDialog(hint, i); | ||
}; | ||
|
||
export const HintsRoot = ({ hint }: HintsRootProps) => { | ||
const hintElements = []; | ||
|
||
for (const [i, hintItem] of hint.getHints().entries()) { | ||
hintElements.push( | ||
HintIcon({ | ||
index: i, | ||
hintItem, | ||
onClick: getHintClick(hint, i), | ||
}) | ||
); | ||
} | ||
|
||
const root = div( | ||
{ | ||
className: hintsClassName, | ||
}, | ||
...hintElements | ||
); | ||
|
||
van.derive(() => { | ||
if (hint._activeHintSignal.val === undefined) return; | ||
|
||
const stepId = hint._activeHintSignal.val; | ||
const hints = hint.getHints(); | ||
const hintItem = hints[stepId]; | ||
|
||
const referenceLayer = ReferenceLayer({ | ||
activeHintSignal: hint._activeHintSignal, | ||
text: hintItem.hint || "", | ||
element: hintItem.element as HTMLElement, | ||
position: hintItem.position, | ||
|
||
helperElementPadding: hint.getOption("helperElementPadding"), | ||
targetElement: hint.getTargetElement(), | ||
|
||
refreshes: hint._refreshes, | ||
|
||
// hints don't have step numbers | ||
showStepNumbers: false, | ||
|
||
autoPosition: hint.getOption("autoPosition"), | ||
positionPrecedence: hint.getOption("positionPrecedence"), | ||
|
||
closeButtonEnabled: hint.getOption("hintShowButton"), | ||
closeButtonLabel: hint.getOption("hintButtonLabel"), | ||
closeButtonClassName: hint.getOption("buttonClass"), | ||
closeButtonOnClick: () => hideHint(hint, stepId), | ||
}); | ||
|
||
van.add(root, referenceLayer); | ||
}); | ||
|
||
return root; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { setPositionRelativeTo } from "../../../util/positionRelativeTo"; | ||
import van, { State } from "../../dom/van"; | ||
import { | ||
hintReferenceClassName, | ||
tooltipReferenceLayerClassName, | ||
} from "../className"; | ||
import { dataStepAttribute } from "../dataAttributes"; | ||
import { HintTooltip, HintTooltipProps } from "./HintTooltip"; | ||
|
||
const { div } = van.tags; | ||
|
||
export type ReferenceLayerProps = HintTooltipProps & { | ||
activeHintSignal: State<number | undefined>; | ||
targetElement: HTMLElement; | ||
helperElementPadding: number; | ||
}; | ||
|
||
export const ReferenceLayer = ({ | ||
activeHintSignal, | ||
targetElement, | ||
helperElementPadding, | ||
...props | ||
}: ReferenceLayerProps) => { | ||
return () => { | ||
// remove the reference layer if the active hint signal is set to undefined | ||
// e.g. when the user clicks outside the hint | ||
if (activeHintSignal.val == undefined) return null; | ||
|
||
const referenceLayer = div( | ||
{ | ||
[dataStepAttribute]: activeHintSignal.val, | ||
className: `${tooltipReferenceLayerClassName} ${hintReferenceClassName}`, | ||
}, | ||
HintTooltip(props) | ||
); | ||
|
||
setTimeout(() => { | ||
setPositionRelativeTo( | ||
targetElement, | ||
referenceLayer, | ||
props.element as HTMLElement, | ||
helperElementPadding | ||
); | ||
}, 1); | ||
|
||
return referenceLayer; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.