Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

feat(tooltip): support for left-right open expanded tooltip#117

Merged
Tigge merged 1 commit intoAxisCommunications:mainfrom
boilund:nm/tooltip-side
Oct 12, 2021
Merged

feat(tooltip): support for left-right open expanded tooltip#117
Tigge merged 1 commit intoAxisCommunications:mainfrom
boilund:nm/tooltip-side

Conversation

@boilund
Copy link
Copy Markdown
Collaborator

@boilund boilund commented Jul 14, 2021

tooltip-leftright

@boilund boilund requested a review from Tigge July 14, 2021 14:09
@boilund boilund force-pushed the nm/tooltip-side branch 2 times, most recently from 79d5491 to e9f36ae Compare August 30, 2021 07:11
@boilund boilund force-pushed the nm/tooltip-side branch 2 times, most recently from 056017e to 0f8738b Compare September 9, 2021 16:14
Comment thread packages/core/src/Tooltip/index.tsx Outdated
@Tigge
Copy link
Copy Markdown
Collaborator

Tigge commented Oct 8, 2021

You can calculate whether if the tooltips fits with this:

    const bounds = anchorEl.getBoundingClientRect()
    const tooltipSize: [number, number] = [
      tooltipEl.clientWidth + 8,
      tooltipEl.clientHeight + 8,
    ]
    const tooltipMid = [
      bounds.left + (bounds.right - bounds.left) / 2,
      bounds.top + (bounds.bottom - bounds.top) / 2,
    ]

    const spaces: Record<Placement, boolean> = {
      down: rectInBounds(
        [tooltipMid[0] - tooltipSize[0] / 2, bounds.bottom],
        tooltipSize
      ),
      up: rectInBounds(
        [tooltipMid[0] - tooltipSize[0] / 2, bounds.top - tooltipSize[1]],
        tooltipSize
      ),
      left: rectInBounds(
        [bounds.left - tooltipSize[0], tooltipMid[1] - tooltipSize[1] / 2],
        tooltipSize
      ),
      right: rectInBounds(
        [bounds.right, tooltipMid[1] - tooltipSize[1] / 2],
        tooltipSize
      ),
    }

And then have fallbacks for each placement:

    if (placement === 'up-down') {
      if (spaces.up || spaces.down) {
        setLayout(spaces.up ? 'up' : 'down')
      } else {
        setLayout(spaces.right ? 'right' : 'left')
      }
    } else if (placement === 'left-right') {
      if (spaces.right || spaces.left) {
        setLayout(spaces.right ? 'right' : 'left')
      } else {
        setLayout(spaces.up ? 'up' : 'down')
      }
    }

@Tigge Tigge merged commit 096fa13 into AxisCommunications:main Oct 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants