diff --git a/docs/components/popup.md b/docs/components/popup.md index 35e4a05b..67c89dd6 100644 --- a/docs/components/popup.md +++ b/docs/components/popup.md @@ -2,7 +2,11 @@ [component-header:sl-popup] -A description of the component goes here. +Popup is a utility that lets you declaratively anchor "popup" containers to another element. + +This component's name is inspired by [``](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/Popup/explainer.md). It uses [Floating UI](https://floating-ui.com/) under the hood to provide a well-tested, lightweight, powerful, and fully declarative positioning utility for tooltips, dropdowns, and more. + +The popup's preferred placement, distance, and skidding (offset) can be configured using attributes. An arrow that points to the anchor can also be shown and customized to your liking. Additional positioning options are described in more detail below. ```html preview @@ -45,17 +47,13 @@ A description of the component goes here. const distance = container.querySelector('sl-input[name="distance"]'); const skidding = container.querySelector('sl-input[name="skidding"]'); const active = container.querySelector('sl-switch[name="active"]'); - const flip = container.querySelector('sl-switch[name="flip"]'); const arrow = container.querySelector('sl-switch[name="arrow"]'); - const fixed = container.querySelector('sl-switch[name="fixed"]'); select.addEventListener('sl-change', () => (popup.placement = select.value)); distance.addEventListener('sl-input', () => (popup.distance = distance.value)); skidding.addEventListener('sl-input', () => (popup.skidding = skidding.value)); active.addEventListener('sl-change', () => (popup.active = active.checked)); - flip.addEventListener('sl-change', () => (popup.flip = flip.checked)); arrow.addEventListener('sl-change', () => (popup.arrow = arrow.checked)); - fixed.addEventListener('sl-change', () => (popup.strategy = fixed.checked ? 'fixed' : 'absolute')); ``` +?> A popup's anchor should never be styled with `display: contents` since the coordinates will not be eligible for calculation. However, if the anchor is a `` element, popup will use the first assigned element as the anchor. This behavior allows other components to pass anchors through more easily via composition. + ## Examples +### Active + +Popups are inactive and hidden until the `active` attribute is applied. Removing the attribute will tear down all positioning logic and listeners, meaning you can have many idle popups on the page without affecting performance. + +```html preview + + + + + +``` + ### Placement -TODO +Use the `placement` attribute to tell the popup the preferred placement of the popup. Note that the actual position will vary to ensure the panel remains in the viewport if you're using positioning features such as `flip` and `shift`. + +```html preview + + + + + +``` ### Distance -TODO +Use the `distance` attribute to change the distance between the popup and its anchor. A positive value will move the popup further away and a negative value will move it closer. + +```html preview + + + + + +``` ### Skidding -TODO +The `skidding` attribute is similar to `distance`, but instead allows you to offset the popup along the anchor's axis. Both positive and negative values are allowed. -### Positioning Strategy +```html preview + -The fixed positioning reduces jumpiness when the anchor is fixed and allows the content to break out containers that clip them. When using this strategy, it's important to note that the content will be positioned _relative to its containing block_, which is usually the viewport unless an ancestor uses a `transform`, `perspective`, or `filter`. [Refer to this page](https://developer.mozilla.org/en-US/docs/Web/CSS/position#fixed) for more details. + -TODO + +``` ### Arrows -TODO +Add an arrow to your popup with the `arrow` attribute. It's usually a good idea to set a `distance` to make room for the arrow. To adjust the arrow's color and size, use the `--arrow-color` and `--arrow-size` custom properties, respectively. You can also target the `arrow` part to add additional styles such as shadows and borders. + +```html preview + + + + + +``` + +### Positioning Strategy + +By default, the popup is positioned using an absolute positioning strategy. However, if your anchor is fixed or exists within a container that has `overflow: auto|hidden`, the popup risks being clipped. To work around this, you can use a fixed positioning strategy by setting the `strategy` attribute to `fixed`. + +The fixed positioning strategy reduces jumpiness when the anchor is fixed and allows the popup to break out containers that clip. When using this strategy, it's important to note that the content will be positioned _relative to its containing block_, which is usually the viewport unless an ancestor uses a `transform`, `perspective`, or `filter`. [Refer to this page](https://developer.mozilla.org/en-US/docs/Web/CSS/position#fixed) for more details. + +In this example, you can see how the popup breaks out of the overflow container when it's fixed. The fixed positioning strategy tends to be less performant than absolute, so avoid using it unnecessarily. + +Toggle the switch and scroll the container to see the difference. + +```html preview + + + + + +``` + +### Flip + +When the popup doesn't have enough room in its preferred placement, it can automatically flip to keep it in view. To enable this, use the `flip` attribute. By default, the popup will flip to the opposite placement, but you can configure preferred fallback placements using `flip-fallback-placement` and `flip-fallback-strategy`. Additional options are available to control the flip behavior's boundary and padding. + +Scroll the container to see how the popup flips to prevent clipping. + +```html preview + + + +``` + +### Shift + +When a popup is longer than its anchor, it risks being clipped by an overflowing container. In this case, use the `shift` attribute to shift the popup along its axis and back into view. You can customize the shift behavior using `shiftBoundary` and `shift-padding`. + +Toggle the switch to see the difference. + +```html preview + + + + + +``` + +### Auto-size + +Use the `auto-size` attribute to tell the popup to resize when necessary to prevent it from overflowing. You can use `autoSizeBoundary` and `auto-size-padding` to customize the behavior of this option. + +For best results, set a preferred width/height on the `popup` part and set `width: 100%; height: 100%;` on your content's wrapper. Auto-size works well with `flip`, but if you're using `auto-size-padding` make sure `flip-padding` is the same value. + +Scroll the container to see auto-size in action. + +```html preview + + + +``` [component-metadata:sl-popup]