Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial review: Document popover="hint" #37990

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The **`popoverTargetElement`** property of the {{domxref("HTMLButtonElement")}}

It is the JavaScript equivalent of the [`popovertarget`](/en-US/docs/Web/HTML/Element/button#popovertarget) HTML attribute.

Associating a popover with a control using the `popoverTargetElement` property creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved

## Value

A reference to a popover element in the DOM.
Expand Down
8 changes: 7 additions & 1 deletion files/en-us/web/api/htmlelement/showpopover/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ When `showPopover()` is called on an element with the [`popover`](/en-US/docs/We

```js-nolint
showPopover()
showPopover(options)
```

### Parameters

None.
- `options` {{optional_inline}}
- : An object that can contain the following properties:
- `source` {{optional_inline}}
- : An {{domxref("HTMLElement")}} reference; programmatically defines the invoker of the popover associated with the toggle action, that is, the control button or link associated with the popover.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is possibly too restrictive language, the source can be any HTMLElement it doesn't have to be just a link or a button. Though those will be the common cases.

It doesn't even have to be the element that was clicked/hovered/etc to trigger showPopover() to be called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In my next commit, I've updated it to just say "...that is, its control element"


Associating a popover with a control using the `source` option creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also enables the nested popover behaviour that popovertarget/commandfor create which is worth a mention.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably link to the nested popover section easy enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to leave the commandfor stuff to another PR, if that's OK. The scope creep on this one is big enough already ;-)

Can you file an issue for this?


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also should do a focus order fixup to put the popover next in the tab order from the source element. This is currently not implemented in Chromium. As such the Browser Compat Data should maybe be marked as a partial implementation? cc @mfreed7 to see what he thinks regarding this.

Copy link
Contributor

@lukewarlow lukewarlow Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mdn/browser-compat-data#25964 - I made a BCD PR to add Safari Tech Preview as partial (due to same bug) and also marked chrome as partial. Happy to change if people think it shouldn't be partial.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this probably makes sense.

### Return value

Expand Down
14 changes: 13 additions & 1 deletion files/en-us/web/api/htmlelement/togglepopover/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ When `togglePopover()` is called on an element with the [`popover`](/en-US/docs/
## Syntax

```js-nolint
togglePopover()
togglePopover(force)
togglePopover(options)
```

### Parameters

- `force`
A boolean (`force`) or an options object:

- `force` {{optional_inline}}
- : A boolean, which causes `togglePopover()` to behave like {{domxref("HTMLElement.showPopover", "showPopover()")}} or {{domxref("HTMLElement.hidePopover", "hidePopover()")}}, except that it doesn't throw an exception if the popover is already in the target state.
- If set to `true`, the popover is shown if it was initially hidden. If it was initially shown, nothing happens.
- If set to `false`, the popover is hidden if it was initially shown. If it was initially hidden, nothing happens.
- `options` {{optional_inline}}
- : An object that can contain the following properties:
- `force` {{optional_inline}}
- : A boolean; see the `force` description above.
- `source` {{optional_inline}}
- : An {{domxref("HTMLElement")}} reference; programmatically defines the invoker of the popover associated with the toggle action, that is, the control button or link associated with the popover.

Associating a popover with a control using the `source` option creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

Copy link
Collaborator

@hamishwillee hamishwillee Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisdavidmills I'm closing all these, but once you've looked at https://github.com/mdn/content/pull/37990/files#r1958808280 you might want to link these to a fixed up section in the Using doc as an intermediate step to getting to the CSS anchor positioning doc.

I.e. "Associating anchor and positioned elements for more details on anchor references." is very general, whereas a specific example for popover hints would be very specific and make things a lot easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, in my next commit, I have updated all of these bits to briefly explain all of the useful additional effects, and link to the relevant sections in the "Using..." guides. I don't want to repeat the entire explanation on each page.


### Return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The **`popoverTargetElement`** property of the {{domxref("HTMLInputElement")}} i

It is the JavaScript equivalent of the [`popovertarget`](/en-US/docs/Web/HTML/Element/button#popovertarget) HTML attribute.

Associating a popover with a control using the `popoverTargetElement` property creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved

## Value

A reference to a popover element in the DOM.
Expand Down
16 changes: 13 additions & 3 deletions files/en-us/web/api/popover_api/using/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ In the below sections, we'll walk through all the important parts of the code.

### Creating the sub-menus with `popover="auto"`

The popup sub-menus are created declaratively, using `auto` popovers. First, the control buttons:
The popup sub-menus are created declaratively, using `auto` popovers.

First, the control buttons:

```html
<section id="button-bar">
Expand Down Expand Up @@ -260,15 +262,15 @@ The event listeners [show](/en-US/docs/Web/API/HTMLElement/showPopover) the popo
```js
function addEventListeners(i) {
btns[i].addEventListener("mouseover", () => {
tooltips[i].showPopover();
tooltips[i].showPopover({ source: btns[i] });
});

btns[i].addEventListener("mouseout", () => {
tooltips[i].hidePopover();
});

btns[i].addEventListener("focus", () => {
tooltips[i].showPopover();
tooltips[i].showPopover({ source: btns[i] });
});

btns[i].addEventListener("blur", () => {
Expand Down Expand Up @@ -334,6 +336,14 @@ The {{cssxref("::backdrop")}} pseudo-element is a full-screen element placed dir

See our [Popover blur background example](https://mdn.github.io/dom-examples/popover-api/blur-background/) ([source](https://github.com/mdn/dom-examples/tree/main/popover-api/blur-background)) for an idea of how this renders.

### Implicit popover anchor associations

Associating a popover with a control using the [`popovertarget`](/en-US/docs/Web/HTML/Element/button#popovertarget) and [`id`](/en-US/docs/Web/HTML/Global_attributes/id) attributes creates an implicit anchor reference between the two, as does programmatically associating a popover action such as {{domxref("HTMLElement.showPopover", "showPopover()")}} with a control using the `source` option.

This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.

For an example that uses this implicit association, see the [Using "hint" popover state](/en-US/docs/Web/API/Popover_API/Using#using_hint_popover_state) section above.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, you're saying that the implicit positioning is already in place, so you don't need to worry about it - a hint will appear in the right place above its associated element?

The first sentence doesn't quite say that "This makes it very convenient" - i.e. it sounds like something you have to do, and that you ware about to describe.

The second sentence says "for an example that uses this implicit association see ... you might say "You can see this implicit associate at work in the exmaple XXX: no special CSS had to be created to position the hints near their associated elements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the association is implicit, not the positioning. The implicit association means you don't need to create an explicit anchor reference, for example using the anchor-name and position-anchor properties.

You still need to do the anchor positioning explicitly, for example, using the anchor() function as values of inset properties, or the position-area property.

I've not made much of a change here, except that I've added a link to the relevant section of the main anchor positioning guide that explains this association, and how the actual positioning is a separate step.

I've also added this link to the other pages that mention the implicit association, as I thought it would be helpful to clear up confusion.

Let me know if you think this needs anything else.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisdavidmills Thanks for explaining - I get it now, but I can completely see why I made this assumption - you show no CSS.

The linked section in "For an example that uses this implicit association, see the Using "hint" popover state section above." does not provide any example. Nor does the rest of the code. Everything says there are implicit links and nothing shows how you style them.

I don't think it is enough to link to the generic styling section. We need a real example. Something like this, copied from the example source code, with explanation:

[popover="hint"] {
  inset: unset;
  position: absolute;
  top: calc(anchor(bottom) + 5px);
  justify-self: anchor-center;
  margin: 0;

  padding: 8px;
  border-radius: 6px;
  background: #271717;
  color: whitesmoke;
  box-shadow: 1px 1px 3px #999;
  border: none;
}

I would also rename https://pr37990.content.dev.mdn.mozit.cloud/en-US/docs/Web/API/Popover_API/Using#implicit_popover_anchor_associations to "Styling hints" - but that's up to you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think styling hints makes sense as a section name fwiw, because you get implicit anchoring for auto and manual popovers too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I understand the problem now! ;-)

So, in my next commit, I have overhauled this section, explaining that you still need to add the positioning CSS code, and showing examples of what this looks like.

I have also removed the link to the earlier section about popover="hint", and just linked straight to the code example on GitHub for an actual real example, so they can just jump into the code and check it out themselves.

I think it is better to not link between these two sections, as it creates a weird circular dependency that is not really needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also added a new section earlier on in the article called "Popover accessibility features", which explains how popover/invoker relationships result in useful ARIA relationships and focus order updates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear you don't get the aria when the invokers are done via JS because it can be any element and we don't know that it makes sense for a given element. But you should get the focus fixup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, perfect, thanks @lukewarlow; I've adjusted my explanation to suit.


## Animating popovers

Popovers are set to `display: none;` when hidden and `display: block;` when shown, as well as being removed from / added to the {{glossary("top layer")}} and the [accessibility tree](/en-US/docs/Web/Performance/How_browsers_work#building_the_accessibility_tree). Therefore, for popovers to be animated, the {{cssxref("display")}} property needs to be animatable. [Supporting browsers](/en-US/docs/Web/CSS/display#browser_compatibility) animate `display` with a variation on the [discrete animation type](/en-US/docs/Web/CSS/CSS_animated_properties#discrete). Specifically, the browser will flip between `none` and another value of `display` so that the animated content is shown for the entire animation duration. So, for example:
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/css/anchor-name/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ anchor-name: unset;

## Description

To position an element relative to an anchor element, the positioned element requires three features: an association, a position, and a location. The `anchor-name` and {{cssxref("position-anchor")}} properties provide the association.
To position an element relative to an anchor element, the positioned element requires three features: an association, a position, and a location. The `anchor-name` and {{cssxref("position-anchor")}} properties provide an explicit association.

The anchor element accepts one or more `<dashed-ident>` anchor names set on it via the `anchor-name` property. When one of those names is then set as the value of the `position-anchor` property of an element that has its {{cssxref("position")}} set to `absolute` or `fixed`, the two elements are associated. The two elements become tethered by setting a location on the associated element relative to the anchor, making it an "anchor-positioned" element.

Expand Down
16 changes: 12 additions & 4 deletions files/en-us/web/css/css_anchor_positioning/using/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Historically, associating an element with another element and dynamically changi

## Associating anchor and positioned elements

To associate an element with an anchor, you need to first declare which element is the anchor, and then specify which positioned element(s) to associate with that anchor. This can be done via CSS or via the HTML [`anchor`](/en-US/docs/Web/HTML/Global_attributes/anchor) attribute.
To associate an element with an anchor, you need to first declare which element is the anchor, and then specify which positioned element(s) to associate with that anchor. This can be done explicitly via CSS or via the HTML [`anchor`](/en-US/docs/Web/HTML/Global_attributes/anchor) attribute, or in some cases, implicitly.

### CSS-only method
### Explicit CSS-only method

To declare an element as an anchor with CSS, you need to set an anchor name on it via the {{cssxref("anchor-name")}} property. The anchor name needs to be a {{cssxref("dashed-ident")}}. In this example, we also set the anchor's {{cssxref("width")}} to `fit-content` to get a small square anchor, which better demonstrates the anchoring effect.

Expand Down Expand Up @@ -90,7 +90,7 @@ This will render as follows:

The anchor and infobox are now associated, but for the moment you'll have to trust us on this. They are not tethered to each other yet — if you were to position the anchor and move it somewhere else on the page, it would move on its own, leaving the infobox in the same place. You'll see the actual tethering in action when we look at [positioning elements based on anchor position](#positioning_elements_relative_to_their_anchor).

### HTML method
### Explicit HTML method

To associate a positioned element with an anchor in HTML, you can use the [`anchor`](/en-US/docs/Web/HTML/Global_attributes/anchor) attribute. You need to give the anchor element an [`id`](/en-US/docs/Web/HTML/Global_attributes/id). The `anchor` attribute is then set on the anchor-positioned element, with a value equal to the `id` of the anchor element you want to associate it with.

Expand Down Expand Up @@ -141,7 +141,15 @@ This gives us the same result that we achieved earlier with CSS. We associated a
> [!NOTE]
> The [`anchor`](/en-US/docs/Web/HTML/Global_attributes/anchor) attribute currently has less support than the CSS equivalent. See the [`anchor` attribute compatibility table](/en-US/docs/Web/HTML/Global_attributes/anchor#browser_compatibility) for more information.

We've associated the two elements, but they are not yet tethered. To tether them together, the positioned element needs to be positioned relative to its anchor, which is done with CSS.
### Implicit anchor association

In some cases, an implicit anchor reference will be made between two elements, due to the semantic nature of their relationship. For example, when using the [Popover API](/en-US/docs/Web/API/Popover_API) to associate a popover with a control, an implicit anchor reference is made between the two. This can occur when:

- Declaratively associating a popover with a control using the [`popovertarget`](/en-US/docs/Web/HTML/Element/button#popovertarget) and [`id`](/en-US/docs/Web/HTML/Global_attributes/id) attributes.
- Programmatically associating a popover action such as {{domxref("HTMLElement.showPopover", "showPopover()")}} with a control using the `source` option.

> [!NOTE]
> The methods above associate an anchor with an element, but they are not yet tethered. To tether them together, the positioned element needs to be positioned relative to its anchor, which is done with CSS.

## Positioning elements relative to their anchor

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/css/position-anchor/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ position-anchor: unset;

This property is only relevant to "positioned" elements — elements and pseudo elements that have a {{cssxref("position")}} of `absolute` or `fixed` set.

To position an element relative to an anchor element, the positioned element requires three features: an association, a position, and a location. The `position-anchor` and {{cssxref("anchor-name")}} properties provide the association.
To position an element relative to an anchor element, the positioned element requires three features: an association, a position, and a location. The `position-anchor` and {{cssxref("anchor-name")}} properties provide an explicit association.

The anchor element accepts one or more `<dashed-ident>` anchor names set on it via the `anchor-name` property. When one of those names is then set as the value of the positioned element's `position-anchor` property, the two elements are associated.

Expand Down
2 changes: 2 additions & 0 deletions files/en-us/web/html/element/button/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ This element's attributes include the [global attributes](/en-US/docs/Web/HTML/G

- : Turns a `<button>` element into a popover control button; takes the ID of the popover element to control as its value. See the {{domxref("Popover API", "Popover API", "", "nocode")}} landing page for more details.

Associating a popover with a control using the `popovertarget` attribute creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.

- `popovertargetaction`

- : Specifies the action to be performed on a popover element being controlled by a control `<button>`. Possible values are:
Expand Down
2 changes: 2 additions & 0 deletions files/en-us/web/html/element/input/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ A few additional non-standard attributes are listed following the descriptions o

- : Turns an `<input type="button">` element into a popover control button; takes the ID of the popover element to control as its value. See the {{domxref("Popover API", "Popover API", "", "nocode")}} landing page for more details.

Associating a popover with a control using the `popovertarget` attribute creates an implicit anchor reference between the two. This makes it very convenient to position popovers relative to their controls using [CSS anchor positioning](/en-US/docs/Web/CSS/CSS_anchor_positioning). Explicit associations do not need to be made using the {{cssxref("anchor-name")}} and {{cssxref("position-anchor")}} properties.
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved

- `popovertargetaction`

- : Specifies the action to be performed on a popover element being controlled by a control `<input type="button">`. Possible values are:
Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/html/global_attributes/popover/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ The **`popover`** [global attribute](/en-US/docs/Web/HTML/Global_attributes) is

The `popover` attribute can take one of the following values:

- `auto` (setting `popover` is equivalent to setting `popover="auto"`)
- `auto`
- `hint`
- `manual`

Setting an empty value for `popover` — `popover` or `popover=""` — is equivalent to setting `popover="auto"`.

## Description

Popover elements are hidden via `display: none` until opened via an invoking/control element (i.e. a `<button>` or `<input type="button">` with a [`popovertarget`](/en-US/docs/Web/HTML/Element/button#popovertarget) attribute) or a {{domxref("HTMLElement.showPopover()")}} call.
Expand Down