import { Story, Meta, html, object, withKnobs } from '@open-wc/demoing-storybook'; import { css } from '@lion/core'; import { tooltipDemoStyles } from './tooltipDemoStyles.js'; import { LionTooltipArrow } from '../src/LionTooltipArrow.js'; import '../lion-tooltip.js'; import '../lion-tooltip-arrow.js'; # Tooltip `lion-tooltip` is a component used for basic popups on hover. Its purpose is to show content appearing when the user hovers over an invoker element with the cursor or with the keyboard, or if th e invoker element is focused. {html`
This is a tooltip
`} ```html
This is a tooltip
``` ## Features - Show content when hovering the invoker - Show content when the invoker is focused - Uses Popper.js under the hood, to have the content pop up relative to the invoker - Use `.config` to override the overlay configuration - Config has `popperConfig` property that has a one to one relation with Popper.js configuration API. - Comes with a default `lion-tooltip-arrow` for an arrow. ## How to use ### Installation ```sh npm i --save @lion/tooltip ``` ```js import '@lion/tooltip/lion-tooltip.js'; ``` ## Examples ### Placements You can easily change the placement of the content node relative to the invoker. {html`
Its top placement
Its right placement
Its bottom placement
Its left placement
`}
```html
Its top placement
``` ### Override Popper configuration You can override the Popper configuration, the API is one to one with Popper's API, and includes modifiers. {html`
This is a tooltip
`} ```js
This is a tooltip
``` Modifier explanations: - keepTogether: prevents detachment of content element from reference element, which could happen if the invoker is not in the viewport anymore. If this is disabled, the content will always be in view. - preventOverflow: enables shifting and sliding behavior on the secondary axis (e.g. if top placement, this is horizontal shifting and sliding). The padding property defines the margin with the boundariesElement, which is usually the viewport. - flip: enables flipping behavior on the primary axis (e.g. if top placement, flipping to bottom if there is not enough space on the top). The padding property defines the margin with the boundariesElement, which is usually the viewport. - offset: enables an offset between the content node and the invoker node. First argument is horizontal marign, second argument is vertical margin. ### Arrow Popper also comes with an arrow modifier. In our tooltip you can pass an arrow element (e.g. an SVG Element) through the `slot="arrow"`. We export a `lion-tooltip-arrow` that you can use by default for this. {html`
This is a tooltip
`} ```html
This is a tooltip
``` #### Use a custom arrow If you plan on passing your own arrow element, you can extend the `lion-tooltip-arrow`. All you need to do is override the `render` method to pass your own SVG, and extend the styles to pass the proper dimensions of your arrow. The rest of the work is done by Popper.js (for positioning) and the `lion-tooltip-arrow` (arrow dimensions, rotation, etc.). {() => { if (!customElements.get('custom-tooltip-arrow')) { customElements.define('custom-tooltip-arrow', class extends LionTooltipArrow { static get styles() { return [super.styles, css` :host { --tooltip-arrow-width: 20px; --tooltip-arrow-height: 8px; } `]; } render() { return html` `; } }); } return html`
This is a tooltip
`; }} ```js import { html, css } from '@lion/core'; import { LionTooltipArrow } from '@lion/tooltip'; class CustomTooltipArrow extends LionTooltipArrow { static get styles() { return [super.styles, css` :host { --tooltip-arrow-width: 20px; --tooltip-arrow-height: 8px; } `]; } render() { return html` `; } } customElements.define('custom-tooltip-arrow', CustomTooltipArrow); ``` ```html
This is a tooltip
``` #### Rationale **Why a Web Component for the Arrow?** Our preferred API is to pass the arrow as a slot. Popper.JS however, necessitates that the arrow element is **inside** the content node, otherwise it cannot compute the positioning of the Popper element and the arrow element, so we move the arrow node inside the content node. This means that we can no longer style the arrow through the `lion-tooltip` with the `::slotted` selector, because the arrow element is no longer a direct child of `lion-tooltip`. Additionally we now also need to sync the popper placement state to the arrow element, to style it properly. For all this logic + style encapsulation, we decided a Web Component was the only reasonable solution for the arrow element.