UNPKG

@contentstack/live-preview-utils

Version:

Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.

1 lines 10.8 kB
{"version":3,"sources":["../../../../src/visualBuilder/components/Tooltip.tsx"],"sourcesContent":["import { h, cloneElement } from 'preact';\nimport { useState, useEffect, useRef } from 'preact/hooks';\nimport {\n computePosition,\n flip,\n shift,\n offset,\n arrow\n} from '@floating-ui/dom';\nimport { visualBuilderStyles } from '../visualBuilder.style';\nimport classNames from 'classnames';\nimport { ContentTypeIcon } from './icons';\nimport { FieldTypeIconsMap } from '../generators/generateCustomCursor';\ninterface TooltipProps {\n children: JSX.Element;\n content: JSX.Element;\n placement?: 'top-start' | 'bottom-start' | 'left-start' | 'right-start';\n}\n\n/**\n * A lightweight, reusable tooltip component for Preact powered by Floating UI.\n *\n * @param {object} props - The component props.\n * @param {preact.ComponentChildren} props.children - The single child element that triggers the tooltip.\n * @param {string | preact.VNode} props.content - The content to display inside the tooltip.\n * @param {'top'|'bottom'|'left'|'right'} [props.placement='top'] - The desired placement of the tooltip.\n */\nconst Tooltip = ({ children, content, placement = 'top-start' }: TooltipProps) => {\n const [isVisible, setIsVisible] = useState(false);\n // Create refs for the trigger and the floating tooltip elements\n const triggerRef = useRef(null);\n const tooltipRef = useRef(null);\n const arrowRef = useRef(null);\n\n const showTooltip = () => setIsVisible(true);\n const hideTooltip = () => setIsVisible(false);\n\n // This effect calculates the tooltip's position whenever it becomes visible\n // or if its content or placement changes.\n useEffect(() => {\n if (!isVisible || !triggerRef.current || !tooltipRef.current) {\n return;\n }\n\n const trigger = triggerRef.current as HTMLElement;\n const tooltip = tooltipRef.current as HTMLElement;\n\n computePosition(trigger, tooltip, {\n placement,\n // Middleware runs in order to modify the position\n middleware: [\n offset(8), // Add 8px of space between the trigger and tooltip\n flip(), // Flip to the opposite side if it overflows\n shift({ padding: 5 }), // Shift to keep it in view\n ...(arrowRef.current ? [arrow({ element: arrowRef.current as HTMLElement })] : []), // Handle arrow positioning\n ],\n }).then(({ x, y, placement, middlewareData }) => {\n // Apply the calculated coordinates to the tooltip element\n Object.assign(tooltip.style, {\n left: `${x}px`,\n top: `${y}px`,\n });\n\n // Position the arrow element\n if (middlewareData.arrow && arrowRef.current) {\n const { x: arrowX, y: arrowY } = middlewareData.arrow;\n const side = placement.split('-')[0];\n const staticSide = {\n top: 'bottom',\n right: 'left',\n bottom: 'top',\n left: 'right',\n }[side] as string;\n\n const arrowElement = arrowRef.current as HTMLElement;\n \n // Reset all positioning properties\n Object.assign(arrowElement.style, {\n left: '',\n top: '',\n right: '',\n bottom: '',\n });\n\n // For placements like top-start, bottom-start, etc., we want the arrow \n // to be centered on the tooltip rather than pointing at the trigger center\n if (placement.includes('-start') || placement.includes('-end')) {\n const tooltipRect = tooltip.getBoundingClientRect();\n \n if (side === 'top' || side === 'bottom') {\n // For top/bottom placements, center the arrow horizontally\n arrowElement.style.left = `${14}px`; // 4px = half arrow width\n if (arrowY != null) {\n arrowElement.style.top = `${arrowY}px`;\n }\n } else {\n // For left/right placements, center the arrow vertically\n arrowElement.style.top = `${tooltipRect.height / 2 - 4}px`; // 4px = half arrow height\n if (arrowX != null) {\n arrowElement.style.left = `${arrowX}px`;\n }\n }\n } else {\n // For regular placements (top, bottom, left, right), use floating-ui's positioning\n if (arrowX != null) {\n arrowElement.style.left = `${arrowX}px`;\n }\n if (arrowY != null) {\n arrowElement.style.top = `${arrowY}px`;\n }\n }\n\n // Position arrow to overlap the tooltip's border\n (arrowElement.style as any)[staticSide] = '-4px';\n }\n });\n\n }, [isVisible, placement, content]);\n\n // We need to clone the child element to attach our ref and event listeners.\n // This ensures we don't wrap the child in an extra <div>.\n const triggerWithListeners = cloneElement(children, {\n ref: triggerRef,\n onMouseEnter: showTooltip,\n onMouseLeave: hideTooltip,\n onFocus: showTooltip,\n onBlur: hideTooltip,\n 'aria-describedby': 'lightweight-tooltip' // for accessibility\n });\n\n return (\n <>\n {triggerWithListeners}\n {isVisible && (\n <div\n ref={tooltipRef}\n role=\"tooltip\"\n id=\"lightweight-tooltip\"\n className={classNames(\"tooltip-container\", visualBuilderStyles()[\"tooltip-container\"])}\n >\n {content}\n <div ref={arrowRef} className={classNames(\"tooltip-arrow\", visualBuilderStyles()[\"tooltip-arrow\"])}></div>\n </div>\n )}\n </>\n );\n};\n\nfunction ToolbarTooltipContent({contentTypeName, referenceFieldName}: {contentTypeName: string, referenceFieldName: string}) {\n return (\n <div className={classNames(\"toolbar-tooltip-content\", visualBuilderStyles()[\"toolbar-tooltip-content\"])}>\n {\n contentTypeName && (\n <div className={classNames(\"toolbar-tooltip-content-item\", visualBuilderStyles()[\"toolbar-tooltip-content-item\"])}>\n <ContentTypeIcon />\n <p>{contentTypeName}</p>\n </div>\n )\n }\n {\n referenceFieldName && (\n <div className={classNames(\"toolbar-tooltip-content-item\", visualBuilderStyles()[\"toolbar-tooltip-content-item\"])}>\n <div dangerouslySetInnerHTML={{__html: FieldTypeIconsMap.reference}} className={classNames(\"visual-builder__field-icon\", visualBuilderStyles()[\"visual-builder__field-icon\"])}/>\n <p>{referenceFieldName}</p>\n </div>\n )\n }\n </div>\n )\n}\n\nexport function ToolbarTooltip({children, data, disabled = false}: {children: JSX.Element, data: {contentTypeName: string, referenceFieldName: string}, disabled?: boolean}) {\n if (disabled) {\n return children;\n }\n const { contentTypeName, referenceFieldName } = data;\n return (\n <Tooltip content={<ToolbarTooltipContent contentTypeName={contentTypeName} referenceFieldName={referenceFieldName} />}>\n {children}\n </Tooltip>\n )\n}\n\nexport default Tooltip;"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAgC;AAChC,mBAA4C;AAC5C,iBAMO;AACP,2BAAoC;AACpC,wBAAuB;AACvB,mBAAgC;AAChC,kCAAkC;AAuH1B;AAxGR,IAAM,UAAU,CAAC,EAAE,UAAU,SAAS,YAAY,YAAY,MAAoB;AAC9E,QAAM,CAAC,WAAW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