import React, { useState } from "react"; import styled from "styled-components"; import Input from "../../components/Input/Input"; import Toggle from "../../components/Toggle/Toggle"; import Text from "../../components/Text/Text"; import HelpIcon from "../../components/Svg/Icons/Help"; import useTooltip from "./useTooltip"; import BalanceInput from "../../components/BalanceInput/BalanceInput"; const GridCell = styled.div` display: flex; align-items: center; justify-content: center; `; const ReferenceElement = styled.div` background-color: #1fc7d4; width: 160px; height: 160px; border-radius: 8px; `; const Container = styled.div` padding: 64px 120px; display: grid; grid-template-columns: repeat(3, 200px); grid-template-rows: repeat(4, 200px); `; const ExpandableCard = styled.div` width: 300px; margin: 0 auto; padding: 0 10px; background-color: #fff; border-radius: 8px; box-shadow: rgba(70, 70, 80, 0.2) 0px 7px 29px 0px; `; const ExpandableHeader = styled.div` height: 40px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; `; export default { title: "Hooks/useTooltip", }; export const Placement: React.FC = () => { // Trigger doesn't matter in this story, it just shows tooltips no matter what // TOP const { targetRef: targetRefTopStart, tooltip: tooltipTopStart } = useTooltip("top-start", { placement: "top-start", }); const { targetRef: targetRefTop, tooltip: tooltipTop } = useTooltip("top", { placement: "top" }); const { targetRef: targetRefTopEnd, tooltip: tooltipTopEnd } = useTooltip("top-end", { placement: "top-end", }); // LEFT const { targetRef: targetRefLeftStart, tooltip: tooltipLeftStart } = useTooltip("left-start", { placement: "left-start", }); const { targetRef: targetRefLeft, tooltip: tooltipLeft } = useTooltip("left", { placement: "left", }); const { targetRef: targetRefLeftEnd, tooltip: tooltipLeftEnd } = useTooltip("left-end", { placement: "left-end" }); // RIGHT const { targetRef: targetRefRightStart, tooltip: tooltipRightStart } = useTooltip("right-start", { placement: "right-start", }); const { targetRef: targetRefRight, tooltip: tooltipRight } = useTooltip("right", { placement: "right" }); const { targetRef: targetRefRightEnd, tooltip: tooltipRightEnd } = useTooltip("right-end", { placement: "right-end", }); // BOTTOM const { targetRef: targetRefBottomStart, tooltip: tooltipBottomStart } = useTooltip("bottom-start", { placement: "bottom-start", }); const { targetRef: targetRefBottom, tooltip: tooltipBottom } = useTooltip("bottom", { placement: "bottom" }); const { targetRef: targetRefBottomEnd, tooltip: tooltipBottomEnd } = useTooltip("bottom-end", { placement: "bottom-end", }); return ( {tooltipTopStart} {tooltipTop} {tooltipTopEnd} {tooltipLeftStart}
{tooltipRightStart} {tooltipLeft}
{tooltipRight} {tooltipLeftEnd}
{tooltipRightEnd} {tooltipBottomStart} {tooltipBottom} {tooltipBottomEnd} ); }; export const Triggers: React.FC = () => { const { tooltipVisible: tooltipVisibleClick, targetRef: targetRefClick, tooltip: tooltipClick, } = useTooltip("You clicked me!", { placement: "right", trigger: "click" }); const { tooltipVisible: tooltipVisibleHover, targetRef: targetRefHover, tooltip: tooltipHover, } = useTooltip("Hovering", { placement: "right", trigger: "hover" }); const { tooltipVisible: tooltipVisibleFocus, targetRef: targetRefFocus, tooltip: tooltipFocus, } = useTooltip("You focused me!", { placement: "right", trigger: "focus" }); return (
{tooltipVisibleClick && tooltipClick} {tooltipVisibleHover && tooltipHover} {tooltipVisibleFocus && tooltipFocus}
); }; export const EventPropagationAndMobile: React.FC = () => { const [showExpandedClick, setShowExpandedClick] = useState(false); const [showExpandedHover, setShowExpandedHover] = useState(false); const { tooltipVisible: tooltipVisibleClick, targetRef: targetRefClick, tooltip: tooltipClick, } = useTooltip("You clicked on the help icon but the card did not expand", { placement: "right", trigger: "click" }); const { tooltipVisible: tooltipVisibleHover, targetRef: targetRefHover, tooltip: tooltipHover, } = useTooltip("You hovered over the help icon", { placement: "right", trigger: "hover" }); return (
Events do not propagate to other elements in the tree. This helps to not cause unwanted bahaviour like expanding the cards when clicking on the tooltip target. setShowExpandedClick(!showExpandedClick)}> On click {showExpandedClick ? "▴" : "▾"} {showExpandedClick && (
You clicked on the header but not on the help icon inside the header
)} {tooltipVisibleClick && tooltipClick}
On touch screen devices hover interactions are also properly handled with `touchstart` and `touchend` events (`mouseenter` and `mouseleave` cause unwated behaviour on some mobile browsers). setShowExpandedHover(!showExpandedHover)}> On hover {showExpandedHover ? "▴" : "▾"} {showExpandedHover && (
On mobile hovering (or more specifically touching and holding) over the help icon does not trigger expansion of this card
)} {tooltipVisibleHover && tooltipHover}
); }; export const FineTuning: React.FC = () => { const { tooltipVisible: tooltipVisibleDefault, targetRef: targetRefDefault, tooltip: tooltipDefault, } = useTooltip("Just default tooltip", { placement: "top-start" }); const { tooltipVisible: tooltipVisibleFineTuned, targetRef: targetRefFineTuned, tooltip: tooltipFineTuned, } = useTooltip("Didn't you know that 6 comes before 7?", { placement: "top-start", arrowPadding: { right: 221 }, tooltipOffset: [0, -8], }); return (
Hover over inputs Default placement {tooltipVisibleDefault && tooltipDefault} Fine tuned arrow placement {tooltipVisibleFineTuned && tooltipFineTuned}
); }; export const Flipping: React.FC = () => { const { targetRef, tooltip } = useTooltip("All tooltips flip automatically when you scroll", { placement: "top" }); return (
{tooltip}
); }; export const ScreenEdges: React.FC = () => { const { targetRef: targetRefLeft, tooltip: tooltipLeft, tooltipVisible: leftVisible, } = useTooltip("I should not touch the edge of the screen", { placement: "top", trigger: "click" }); const { targetRef: targetRefRight, tooltip: tooltipRight, tooltipVisible: rightVisible, } = useTooltip("I should not touch the edge of the screen", { placement: "top", trigger: "click" }); const { targetRef: targetRefMiddle, tooltip: tooltipMiddle, tooltipVisible: middleVisible, } = useTooltip("I should not touch the edge of the screen", { placement: "top", trigger: "click" }); return (
This story can be used to visually tooltip behavior when the target element is positioned close to the screen edge. Open this screen on the phone or in browser with responsive mode. Tooltips should not touch the screen edge.
{leftVisible && tooltipLeft} {middleVisible && tooltipMiddle} {rightVisible && tooltipRight}
); }; export const ThemeInversion: React.FC = () => { const tooltipContent = ( <> Tooltips have inverted theme ); const { targetRef, tooltip } = useTooltip(tooltipContent, { placement: "bottom" }); return (
Current theme looks like this
{tooltip}
); };