From ce548696c6b1cb82d1b3b83c6e5b9a0a666018c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kry=C5=A1p=C3=ADn?= Date: Thu, 7 Dec 2023 16:41:41 +0100 Subject: [PATCH] Feat(web-twig): Introduce Full Floating UI demo to Tooltip, update internal implementation #DS-1084 Switch to data attribute placement instead of modification class based. --- .../components/Tooltip/Tooltip.stories.twig | 56 +++--- .../Resources/components/Tooltip/Tooltip.twig | 36 +--- .../__tests__/__fixtures__/tooltip.twig | 2 +- .../__snapshots__/tooltip.twig.snap.html | 4 +- .../Tooltip/stories/TooltipFloatingUI.twig | 186 +++++++++++++----- .../Tooltip/stories/TooltipPlacements.twig | 30 +-- 6 files changed, 195 insertions(+), 119 deletions(-) diff --git a/packages/web-twig/src/Resources/components/Tooltip/Tooltip.stories.twig b/packages/web-twig/src/Resources/components/Tooltip/Tooltip.stories.twig index a7232959d6..5e9b9f12ea 100644 --- a/packages/web-twig/src/Resources/components/Tooltip/Tooltip.stories.twig +++ b/packages/web-twig/src/Resources/components/Tooltip/Tooltip.stories.twig @@ -2,32 +2,34 @@ {% block content %} - - {% include '@components/Tooltip/stories/TooltipPlacements.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipStatic.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipOnHover.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipClickable.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipDismissible.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipDismissibleViaJS.twig' %} - - - - {% include '@components/Tooltip/stories/TooltipFloatingUI.twig' %} - +
+ + {% include '@components/Tooltip/stories/TooltipPlacements.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipStatic.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipOnHover.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipClickable.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipDismissible.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipDismissibleViaJS.twig' %} + + + + {% include '@components/Tooltip/stories/TooltipFloatingUI.twig' %} + +
{% endblock %} diff --git a/packages/web-twig/src/Resources/components/Tooltip/Tooltip.twig b/packages/web-twig/src/Resources/components/Tooltip/Tooltip.twig index cd5bb75687..fa2c816688 100644 --- a/packages/web-twig/src/Resources/components/Tooltip/Tooltip.twig +++ b/packages/web-twig/src/Resources/components/Tooltip/Tooltip.twig @@ -10,48 +10,24 @@ {%- set _closeClassName = _spiritClassPrefix ~ 'Tooltip__close' -%} {%- set _rootClassName = _spiritClassPrefix ~ 'Tooltip' -%} {%- set _rootDismissibleClassName = _isDismissible == 'true' ? _spiritClassPrefix ~ 'Tooltip--dismissible' : null -%} -{%- set _topClassName = _spiritClassPrefix ~ 'Tooltip--top' -%} -{%- set _topLeftClassName = _spiritClassPrefix ~ 'Tooltip--topLeft' -%} -{%- set _topRightClassName = _spiritClassPrefix ~ 'Tooltip--topRight' -%} -{%- set _bottomClassName = _spiritClassPrefix ~ 'Tooltip--bottom' -%} -{%- set _bottomLeftClassName = _spiritClassPrefix ~ 'Tooltip--bottomLeft' -%} -{%- set _bottomRightClassName = _spiritClassPrefix ~ 'Tooltip--bottomRight' -%} -{%- set _leftClassName = _spiritClassPrefix ~ 'Tooltip--left' -%} -{%- set _leftTopClassName = _spiritClassPrefix ~ 'Tooltip--leftTop' -%} -{%- set _leftBottomClassName = _spiritClassPrefix ~ 'Tooltip--leftBottom' -%} -{%- set _rightClassName = _spiritClassPrefix ~ 'Tooltip--right' -%} -{%- set _rightTopClassName = _spiritClassPrefix ~ 'Tooltip--rightTop' -%} -{%- set _rightBottomClassName = _spiritClassPrefix ~ 'Tooltip--rightBottom' -%} {# Attributes #} {%- set _idAttr = _id ? 'id="' ~ _id | escape('html_attr') ~ '"' : null -%} {%- set _ariaControlsAttr = _id ? 'aria-controls="' ~ _id | escape('html_attr') ~ '"' : null -%} +{%- set _dataPlacementAttr = _placement ? 'data-spirit-placement="' ~ _placement | escape('html_attr') ~ '"' : null -%} {%- set _dataTargetAttr = _id ? 'data-spirit-target="#' ~ _id | escape('html_attr') ~ '"' : null -%} {# Miscellaneous #} {%- set _styleProps = useStyleProps(props) -%} -{%- set _placementClassNames = { - 'top': _topClassName, - 'top-left': _topLeftClassName, - 'top-right': _topRightClassName, - 'bottom': _bottomClassName, - 'bottom-left': _bottomLeftClassName, - 'bottom-right': _bottomRightClassName, - 'left': _leftClassName, - 'left-top': _leftTopClassName, - 'left-bottom': _leftBottomClassName, - 'right': _rightClassName, - 'right-top': _rightTopClassName, - 'right-bottom': _rightBottomClassName, -} -%} -{%- set _classNames = [ _rootClassName, _rootDismissibleClassName, _placementClassNames[_placement], _styleProps.className ] -%} -{%- set _mainPropsWithoutId = props | filter((value, prop) => prop is not same as('id')) -%} +{%- set _classNames = [ _rootClassName, _rootDismissibleClassName, _styleProps.className ] -%} +{%- set _mainPropsWithoutReservedAttributes = props | filter((value, prop) => prop not in ['id', 'data-spirit-placement']) -%}
{% block content %}{% endblock %} {% if _isDismissible == 'true' %} @@ -67,5 +43,5 @@ {{ _closeLabel }} {% endif %} - +
diff --git a/packages/web-twig/src/Resources/components/Tooltip/__tests__/__fixtures__/tooltip.twig b/packages/web-twig/src/Resources/components/Tooltip/__tests__/__fixtures__/tooltip.twig index 9fb0a611df..355d7b2a19 100644 --- a/packages/web-twig/src/Resources/components/Tooltip/__tests__/__fixtures__/tooltip.twig +++ b/packages/web-twig/src/Resources/components/Tooltip/__tests__/__fixtures__/tooltip.twig @@ -5,7 +5,7 @@ closeLabel="Close tooltip" id="my-tooltip" isDismissible - placement="right-top" + placement="right-start" > Hello there! diff --git a/packages/web-twig/src/Resources/components/Tooltip/__tests__/__snapshots__/tooltip.twig.snap.html b/packages/web-twig/src/Resources/components/Tooltip/__tests__/__snapshots__/tooltip.twig.snap.html index ed5610bf8f..8ff7ee1cb2 100644 --- a/packages/web-twig/src/Resources/components/Tooltip/__tests__/__snapshots__/tooltip.twig.snap.html +++ b/packages/web-twig/src/Resources/components/Tooltip/__tests__/__snapshots__/tooltip.twig.snap.html @@ -5,12 +5,12 @@ -
+
Hello there!
-
+
Hello there! diff --git a/packages/web-twig/src/Resources/components/Tooltip/stories/TooltipFloatingUI.twig b/packages/web-twig/src/Resources/components/Tooltip/stories/TooltipFloatingUI.twig index 3e74cacbb3..82f925d916 100644 --- a/packages/web-twig/src/Resources/components/Tooltip/stories/TooltipFloatingUI.twig +++ b/packages/web-twig/src/Resources/components/Tooltip/stories/TooltipFloatingUI.twig @@ -1,62 +1,160 @@ +

+ The following example is using external library Floating UI. + Try scrolling the frame or resizing the window to see how the Tooltip behaves. The Floating UI + library is trying to keep the Tooltip in the viewport and it is also flipping, shifting and + resizing the Tooltip when it is not possible to keep it in the viewport. +

+ + + Please note that the Floating UI library is trying to point the arrow to the center + of the trigger element. This is not possible to achieve in CSS only so our behavior + is slightly different for tooltips not using Floating UI. + + + + -

- The following example is using external library Floating UI. -

-

🖱 Try scrolling the example to see how Tooltip placement is updated.

- -
-
- - - Hello there! - - -
+
+
+ + + This long tooltip is flipping, resizing and shifting to stay in the viewport. + Also its arrow is always trying to point to the center of the trigger. + +