-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooltip)!: add post-tooltip-trigger
In order to get rid of mutation observers on attributes, a new element to wrap the trigger was added for the tooltip. This enables easier lifecycle management and more precise event listeners with less overhead.
- Loading branch information
Showing
14 changed files
with
283 additions
and
190 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,5 @@ | ||
--- | ||
'@swisspost/design-system-components': major | ||
--- | ||
|
||
Removed the arrow option for the `<post-tooltip>` element, the arrow is always shown for tooltips. |
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,5 @@ | ||
--- | ||
'@swisspost/design-system-components': major | ||
--- | ||
|
||
Updated the trigger for the `<post-tooltip>`. Instead of using an attribute to link a trigger element with the tooltip, a new `<post-tooltip-trigger>` element is available for wrapping the element that should display the tooltip. |
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
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
70 changes: 70 additions & 0 deletions
70
packages/components/src/components/post-tooltip-trigger/post-tooltip-trigger.tsx
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,70 @@ | ||
import { Component, Element, Prop } from '@stencil/core'; | ||
import { isFocusable } from '../../utils/is-focusable'; | ||
import { version } from '@root/package.json'; | ||
import 'long-press-event'; | ||
|
||
@Component({ | ||
tag: 'post-tooltip-trigger', | ||
}) | ||
export class PostTooltipTrigger { | ||
/** | ||
* Link the trigger to a tooltip with this id | ||
*/ | ||
@Prop() for: string; | ||
|
||
@Element() host: HTMLPostTooltipTriggerElement; | ||
|
||
private trigger: HTMLElement; | ||
private localInterestHandler; | ||
private localInterestLostHandler; | ||
|
||
constructor() { | ||
this.localInterestHandler = this.interestHandler.bind(this); | ||
this.localInterestLostHandler = this.interestLostHandler.bind(this); | ||
} | ||
|
||
componentDidLoad() { | ||
this.host.setAttribute('data-version', version); | ||
|
||
if (this.host?.children.length > 0 && this.host.children[0].nodeType === 1) { | ||
this.trigger = this.host.children[0] as HTMLElement; | ||
} else { | ||
this.trigger = this.host; | ||
} | ||
|
||
// Ensure trigger is focusable | ||
if (!isFocusable(this.trigger)) { | ||
this.trigger.setAttribute('tabindex', '0'); | ||
} | ||
|
||
// Add tooltip to aria-describedby | ||
const describedBy = this.trigger.getAttribute('aria-describedby'); | ||
if (!describedBy?.includes(this.for)) { | ||
const newDescribedBy = describedBy ? `${describedBy} ${this.for}` : this.for; | ||
this.trigger.setAttribute('aria-describedby', newDescribedBy); | ||
} | ||
|
||
this.host.addEventListener('pointerover', this.localInterestHandler); | ||
this.host.addEventListener('pointerout', this.localInterestLostHandler); | ||
this.host.addEventListener('focusin', this.localInterestHandler); | ||
this.host.addEventListener('focusout', this.localInterestLostHandler); | ||
this.host.addEventListener('long-press', this.localInterestHandler); | ||
} | ||
|
||
private get tooltip(): HTMLPostTooltipElement | null { | ||
const ref = document.getElementById(this.for); | ||
if (ref && ref.tagName === 'POST-TOOLTIP') { | ||
return ref as HTMLPostTooltipElement; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private interestHandler() { | ||
this.tooltip?.show(this.trigger); | ||
} | ||
|
||
private interestLostHandler() { | ||
this.tooltip?.hide(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/components/src/components/post-tooltip-trigger/readme.md
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,17 @@ | ||
# post-tooltip-trigger | ||
|
||
|
||
|
||
<!-- Auto Generated Below --> | ||
|
||
|
||
## Properties | ||
|
||
| Property | Attribute | Description | Type | Default | | ||
| -------- | --------- | ------------------------------------------ | -------- | ----------- | | ||
| `for` | `for` | Link the trigger to a tooltip with this id | `string` | `undefined` | | ||
|
||
|
||
---------------------------------------------- | ||
|
||
*Built with [StencilJS](https://stenciljs.com/)* |
Oops, something went wrong.