1. 6.11 popover属性
      1. 6.11.1 ポップオーバーターゲット属性
      2. 6.11.2 ポップオーバーの簡易非表示
      3. 6.11.3 ToggleEventインターフェイス

6.11 popover属性

すべてのHTML要素popoverコンテンツ属性設定を持ってもよい。指定された場合、要素は表示されるまでレンダリングされず、表示されると、他のページ コンテンツの上にレンダリングされる。

The popover attribute is a global attribute that allows authors flexibility to ensure popover functionality can be applied to elements with the most relevant semantics.

The following demonstrates how one might create a popover sub-navigation list of links, within the global navigation for a website.

<ul>
  <li>
    <a href=...>All Products</a>
    <button popovertarget=sub-nav>
     <img src=down-arrow.png alt="Product pages">
    </button>
    <ul popover id=sub-nav>
     <li><a href=...>Shirts</a>
     <li><a href=...>Shoes</a>
     <li><a href=...>Hats etc.</a>
    </ul>
  </li>
  <!-- other list items and links here -->
</ul>

When using popover on elements without accessibility semantics, for instance the div element, authors should use the appropriate ARIA attributes to ensure the popover is accessible.

The following shows the baseline markup to create a custom menu popover, where the first menuitem will receive keyboard focus when the popover is invoked due to the use of the autofocus attribute. Navigating the menuitems by arrow keys, and activation behaviors would still need author scripting.

<button popovertarget=menu aria-haspopup=menu>Actions</button>
 <div role=menu id=menu popover>
  <button role=menuitem tabindex=-1 autofocus>Edit</button>
  <button role=menuitem tabindex=-1>Hide</button>
  <button role=menuitem tabindex=-1>Delete</button>
</div>

A popover can be useful for rendering a status message, confirming the action performed by the user. The following demonstrates how one could reveal a popover in an output element.

<button id=submit>Submit</button>
<p><output><span popover=manual></span></output></p>

<script>
 const sBtn = document.getElementById('submit');
 const outSpan = document.querySelector('output [popover=manual]');
 let successMessage;
 let errorMessage;

 /* define logic for determining success of action
  and determining the appropriate success or error
  messages to use */

 sBtn.addEventListener('click', ()=> {
  if ( success ) {
   outSpan.textContent = successMessage;
  }
  else {
   outSpan.textContent = errorMessage;
  }
  outSpan.showPopover();

  setTimeout(function () {
   outSpan.hidePopover();
  }, 10000);
 });
<script>

Inserting a popover element into an output element will generally cause screen readers to announce the content when it becomes visible. Depending on the complexity or frequency of the content, this could be either useful or annoying to users of these assistive technologies. Keep this in mind when using the output element or other ARIA live regions to ensure the best user experience.

popover属性は、次のキーワードと状態を持つ列挙属性である:

キーワード状態概要
autoauto開いたときに他のポップオーバーを閉じる。簡易非表示を持つ。
空文字列
manualmanual他のポップオーバーを閉じない。簡易非表示を持たない。

popover属性の不正値のデフォルトmanual状態であり、その欠損値のデフォルトポップオーバーなし状態である。

popover IDL属性は、既知の値に制限されpopover属性を反映しなければならない。

すべての HTML要素は、ポップオーバー可視状態を持ち、最初は非表示で、次の潜在的な値をもつ:

Documentは、自動ポップオーバーリストを持つ。これは、Document最上位レイヤーにあるすべての要素のリストであり、そのpopover属性は、同じ順序でauto状態にある。

Documentは、ポップオーバーポインターダウンターゲットを持ち、これはHTML要素またはnullであり、最初はnullである。

すべてのHTML要素は、ポップオーバー呼び出しを持ち、これはHTML要素またはnullであり、最初はnullに設定される。

すべてのHTML要素は、ポップオーバートリガータスクを持ち、最初はnullであり、これはnullまたは以下を持つ構造体のいずれかである:

Task
ToggleEventを発火するタスク。
Old state
oldState属性のタスクのイベントの値を表す文字列。

The following attribute change steps are used for all HTML elements:

  1. If namespace is not null, then return.

  2. If localName is not popover, then return.

  3. If oldValue and value are in different states, then run the hide popover algorithm given element, true, true, and false.

element.showPopover()
ポップオーバーelementをトップレイヤーに追加して表示する。elementpopover属性がauto stateである場合、トップレベルのポップオーバー祖先アルゴリズムに従ってelementの祖先でない限り、他のすべてのautoポップオーバーも閉じられる。
element.hidePopover()
ポップオーバーelementをトップレイヤーから削除し、display:noneを適用して非表示にする。
element.togglePopover()
ポップオーバーelementが表示されていない場合、このメソッドはそのポップオーバーを表示する。そうでなければ、このメソッドによって非表示になる。

The showPopover() method steps are:

  1. Run show popover given this and true.

To show popover, given an HTML element element and a boolean throwExceptions:

  1. If the result of running check popover validity given element, false, and throwExceptions is false, then return.

  2. Assert: element is not in element's node document's top layer.

  3. If the result of firing an event named beforetoggle, using ToggleEvent, with the cancelable attribute initialized to true, the oldState attribute initialized to "closed", and the newState attribute initialized to "open" at element is false, then return.

  4. If the result of running check popover validity given element, false, and throwExceptions is false, then return.

    Check popover validity is called again because firing the beforetoggle event could have disconnected this element or changed its popover attribute.

  5. Let document be element's node document.

  6. Let shouldRestoreFocus be false.

  7. If element's popover attribute is in the auto state, then:

    1. Let originalType be the value of element's popover attribute.

    2. Let ancestor be the result of running the topmost popover ancestor algorithm given element.

    3. If ancestor is null, then set ancestor to document.

    4. Run hide all popovers until given ancestor, false, and true.

    5. If originalType is not equal to the value of element's popover attribute, or if the result of running check popover validity given element, false, and throwExceptions is false, then return.

      Check popover validity is called again because running hide all popovers until above could have fired the beforetoggle event, and an event handler could have disconnected this element or called showPopover() on this element.

  8. If the result of running topmost auto popover on document is null, then set shouldRestoreFocus to true.

    This ensures that focus is returned to the previously-focused element only for the first popover in a stack.

  9. Set element's previously focused element to null.

  10. Let originallyFocusedElement be document's focused area of the document's DOM anchor.

  11. Add element to document's top layer.

  12. Set element's popover visibility state to showing.

  13. Run the popover focusing steps given element.

  14. If shouldRestoreFocus is true and element's popover attribute is not in the no popover state, then set element's previously focused element to originallyFocusedElement.

  15. Queue a popover toggle event task given element, "closed", and "open".

To queue a popover toggle event task given an element element, a string oldState, and a string newState:

  1. If element's popover toggle task is not null, then:

    1. Set oldState to element's popover toggle task's old state.

    2. Remove element's popover toggle task's task from its task queue.

    3. Set element's popover toggle task to null.

  2. Queue an element task given the user interaction task source and element to run the following steps:

    1. fire an event named toggle, using ToggleEvent, with the oldState attribute initialized to oldState, and the newState attribute initialized to newState at element.

    2. Set element's popover toggle task to null.

  3. Set element's popover toggle task to a struct with task set to the just-queued task and old state set to oldState.

The hidePopover() method steps are:

  1. Run the hide popover algorithm given this, true, true, and true.

To hide a popover given an HTML element element, a boolean focusPreviousElement, a boolean fireEvents, and a boolean throwExceptions:

  1. If the result of running check popover validity given element, true, and throwExceptions is false, then return.

  2. Let document be element's node document.

  3. If element's popover attribute is in the auto state, then:

    1. Run hide all popovers until given element, focusPreviousElement, and fireEvents.

    2. If the result of running check popover validity given element, true, and throwExceptions is false, then return.

      Check popover validity is called again because running hide all popovers until could have disconnected element or changed its popover attribute.

    3. Assert: the last item in document's auto popover list is element.

  4. Set element's popover invoker to null.

  5. If fireEvents is true:

    1. Fire an event named beforetoggle, using ToggleEvent, with the oldState attribute initialized to "open", and the newState attribute initialized to "closed" at element.

    2. If the result of running check popover validity given element, true, and throwExceptions is false, then return.

      Check popover validity is called again because firing the beforetoggle event could have disconnected element or changed its popover attribute.

  6. Remove element from the top layer.

  7. Set element's popover visibility state to hidden.

  8. If fireEvents is true, then queue a popover toggle event task given element, "open", and "closed".

  9. Let previouslyFocusedElement be element's previously focused element.

  10. If previouslyFocusedElement is not null, then:

    1. Set element's previously focused element to null.

    2. If focusPreviousElement is true, then run the focusing steps for previouslyFocusedElement; the viewport should not be scrolled by doing this step.

The togglePopover(force) method steps are:

  1. If this's popover visibility state is showing, and force is not present or false, then run the hide popover algorithm given this, true, true, and true.

  2. Otherwise, if force is not present or true, then run show popover given this and true.

To hide all popovers until, given an HTML element or Document endpoint, a boolean focusPreviousElement, and a boolean fireEvents:

  1. Let document be endpoint's node document.

  2. Let closeAllOpenPopovers be an algorithm which performs the following steps:

    1. Let popover be document's topmost auto popover.

    2. While popover is not null:

      1. Run the hide popover algorithm given popover, focusPreviousElement, fireEvents, and false.

      2. Set popover to document's topmost auto popover.

  3. If endpoint is a Document, then run closeAllOpenPopovers and return.

  4. Let lastToHide be null.

  5. Let foundEndpoint be false.

  6. For each popover in document's auto popover list:

    1. If popover is endpoint, then set foundEndpoint to true.

    2. Otherwise, if foundEndpoint is true, then set lastToHide to popover and break.

  7. If foundEndpoint is false, then run closeAllOpenPopovers and return.

  8. While lastToHide is not null and lastToHide's popover visibility state is showing and document's auto popover list is not empty:

    1. Run the hide popover algorithm given document's auto popover list's last element, focusPreviousElement, fireEvents, and false.

The hide all popovers until algorithm is used in several cases to hide all popovers that don't stay open when something happens. For example, during light-dismiss of a popover, this algorithm ensures that we close only the popovers that aren't related to the node clicked by the user.

To hide all popovers, given a Document document, run hide all popovers until given document, false, and false.

To find the topmost popover ancestor, given a Node newPopover, perform the following steps. They return an HTML element or null.

The topmost popover ancestor algorithm will return the topmost (latest in the auto popover list) ancestor popover for the provided popover. Popovers can be related to each other in several ways, creating a tree of popovers. There are two paths through which one popover (call it the "child" popover) can have a topmost ancestor popover (call it the "parent" popover):

  1. The popovers are nested within each other in the node tree. In this case, the descendant popover is the "child" and its topmost ancestor popover is the "parent".

  2. An invoking element (e.g., a button) has a popovertarget attribute pointing to a popover. In this case, the popover is the "child", and the popover subtree the invoking element is in is the "parent". The invoker has to be in a popover and reference an open popover.

In each of the relationships formed above, the parent popover has to be strictly earlier in the auto popover list than the child popover, or it does not form a valid ancestral relationship. This eliminates non-showing popovers and self-pointers (e.g., a popover with an anchor attribute that points back to the same popover), and it allows for the construction of a well-formed tree from the (possibly cyclic) graph of connections. For example, if two popovers have anchors pointing to each other, the only valid relationship is that the first one to open is the "parent" and the second is the "child". Only auto popovers are considered.

  1. Let popoverPositions be an empty map.

  2. indexを0にする。

  3. Let document be newPopover's node document.

  4. For each popover in document's auto popover list:

    1. Set popoverPositions[popover] to index.

    2. Increment index by 1.

  5. Set popoverPositions[newPopover] to index.

  6. Increment index by 1.

  7. Let topmostPopoverAncestor be null.

  8. Let checkAncestor be an algorithm which performs the following steps given candidate:

    1. If candidate is null, then return.

    2. Let candidateAncestor be the result of running nearest inclusive open popover given candidate.

    3. If candidateAncestor is null, then return.

    4. Let candidatePosition be popoverPositions[candidateAncestor].

    5. If topmostPopoverAncestor is null or popoverPositions[topmostPopoverAncestor] is less than candidatePosition, then set topmostPopoverAncestor to candidateAncestor.

  9. Run checkAncestor given the result of running nearest inclusive open popover given newPopover's parent node within the flat tree.

  10. For each button invoker that is a descendant of newPopover's root, in tree order:

    1. If invoker's popover target element is newPopover, then run checkAncestor given invoker.

  11. return topmostPopoverAncestor.

To find the nearest inclusive open popover given a Node node, perform the following steps. They return an HTML element or null.

  1. Let currentNode be node.

  2. While currentNode is not null:

    1. If currentNode's popover attribute is in the auto state and currentNode's popover visibility state is showing, then return currentNode.

    2. Set currentNode to currentNode's parent in the flat tree.

  3. Return null.

To find the topmost auto popover given a Document document, perform the following steps. They return an HTML element or null.

  1. If document's auto popover list is not empty, then return document's auto popover list's last element.

  2. Return null.

To perform the popover focusing steps for an HTML element subject:

  1. Let control be the focus delegate of subject given "other" and true.

  2. If control is null, then return.

  3. Run the focusing steps given control.

  4. Let topDocument be the active document of control's node document's browsing context's top-level browsing context.

  5. If control's node document's origin is not the same as the origin of topDocument, then return.

  6. Empty topDocument's autofocus candidates.

  7. Set topDocument's autofocus processed flag to true.

To check popover validity for an HTML element element given a boolean expectedToBeShowing and a boolean throwExceptions, perform the following steps. They throw an exception or return a boolean.

  1. If element's popover attribute is in the no popover state, then:

    1. If throwExceptions is true, then throw a "NotSupportedError" DOMException.

    2. falseを返す。

  2. If one of the following conditions is true

    then:

    1. If throwExceptions is true, then throw a "InvalidStateError" DOMException.

    2. falseを返す。

  3. Return true.

6.11.1 ポップオーバーターゲット属性

ボタンには、次のコンテンツ属性を設定してもよい:

指定された場合、popovertarget属性値は、popovertarget属性をもつボタンと同じツリー内のpopover属性をもつ要素のIDでなければならない。

popovertargetaction属性は、次のキーワードと状態を持つ列挙属性である:

キーワード状態概要
toggletoggleターゲットのポップオーバー要素を表示または非表示する。
showshowターゲットのポップオーバー要素を表示する。
hidehideターゲットのポップオーバー要素を非表示にする。

popovertargetaction属性の不正値のデフォルト欠損値のデフォルトの両方は、トグル状態である。

Whenever possible ensure the popover element is placed immediately after its triggering element in the DOM. Doing so will help ensure that the popover is exposed in a logical programmatic reading order for users of assistive technology, such as screen readers.

The following shows how the popovertarget attribute in combination with the popovertargetaction attribute can be used to show and close a popover:

<button popovertarget="foo" popovertargetaction="show">
  Show a popover
</button>

<article popover=auto id="foo">
  This is a popover article!
  <button popovertarget="foo" popovertargetaction="close">Close</button>
</article>

If a popovertargetaction attribute is not specified, the default action will be to toggle the associated popover. The following shows how only specifying the popovertarget attribute on its invoking button can toggle a manual popover between its opened and closed states. A manual popover will not light dismiss:

<input type="button" popovertarget="foo" value="Toggle the popover">

<div popover=manual id="foo">
  This is a popover!
</div>
DOMインターフェイス

popoverTargetElement IDL属性は、popovertarget属性を反映しなければならない。

popoverTargetAction IDL属性は、既知の値のみに制限されるpopoverTargetAction属性を反映しなければならない。

To run the popover target attribute activation behavior given a Node node:

  1. Let popover be node's popover target element.

  2. If popover is null, then return.

  3. If node's popovertargetaction attribute is in the show state and popover's popover visibility state is showing, then return.

  4. If node's popovertargetaction attribute is in the hide state and popover's popover visibility state is hidden, then return.

  5. If popover's popover visibility state is showing, then run the hide popover algorithm given popover, true, true, and false.

  6. Otherwise, if popover's popover visibility state is hidden and the result of running check popover validity given popover, false, and false is true:

    1. Set popover's popover invoker to node.

    2. Run show popover given popover and false.

To get the popover target element given a Node node, perform the following steps. They return an HTML element or null.

  1. If node is not a button, then return null.

  2. If node is disabled, then return null.

  3. If node has a form owner and node is a submit button, then return null.

  4. Let popoverElement be node's popovertarget-associated element.

  5. If popoverElement is null, then return null.

  6. If popoverElement's popover attribute is in the no popover state, then return null.

  7. Return popoverElement.

6.11.2 ポップオーバーの簡易非表示

"簡易非表示"とは、Escキーを押すか、popover属性がauto状態のポップオーバーの外側をクリックするとポップオーバーが閉じることを意味する。

ポップオーバーのキャンセルDocument最上位の自動ポップオーバーが表示されている場合、ユーザー エージェントは、アクティブ化時に、最上位の自動ポップオーバー、trueおよびfalseが指定されたユーザーインタラクションタスクソース要素タスクをキューに入れ最上位の自動ポップオーバーが指定された非表示のポップオーバーアルゴリズムを実行するユーザーインターフェイスを提供してもよい。

To light dismiss open popovers, given an Event event:

  1. Assert: event's isTrusted attribute is true.

  2. Let target be event's target.

  3. Let topmostPopover be the result of running topmost auto popover given target.

  4. If topmostPopover is null, then return.

  5. Let document be target's node document.

  6. If event is a PointerEvent and event's type is pointerdown, then: set document's popover pointerdown target to the result of running topmost clicked popover given target.

  7. If event is a PointerEvent and event's type is pointerup, then:

    1. Let ancestor be the result of running topmost clicked popover given target.

    2. Let sameTarget be true if ancestor is document's popover pointerdown target.

    3. Set document's popover pointerdown target to null.

    4. If ancestor is null, then set ancestor to document.

    5. If sameTarget is true, then run hide all popovers until given ancestor, false, and true.

Light dismiss open popovers will be called by the Pointer Events spec when the user clicks or touches anywhere on the page.

To find the topmost clicked popover, given a Node node:

  1. Let clickedPopover be the result of running nearest inclusive open popover given node.

  2. Let invokerPopover be the result of running nearest inclusive target popover for invoker given node.

  3. Let getStackPosition be an algorithm which performs the following steps given an HTML element popover:

    1. Let popoverList be popover's node document's auto popover list.

    2. If popover is in popoverList, then return the index of popover in popoverList + 1.

    3. Return 0.

  4. If the result of running getStackPosition given clickedPopover is greater than the result of running getStackPosition given invokerPopover, then return clickedPopover.

  5. Return invokerPopover.

To find the nearest inclusive target popover for invoker given a Node node:

  1. Let currentNode be node.

  2. While currentNode is not null:

    1. Let targetPopover be currentNode's popover target element.

    2. If targetPopover is not null and targetPopover's popover attribute is in the auto state and targetPopover's popover visibility state is showing, then return targetPopover.

    3. Set currentNode to currentNode's ancestor in the flat tree.

6.11.3 ToggleEventインターフェイス

event.oldState

クローズからオープンに遷移する場合は"closed"に設定し、オープンからクローズに遷移する場合は"open"に設定する。

event.newState

クローズからオープンに遷移する場合は"open"に設定し、オープンからクローズに遷移する場合は"closed"に設定する。

oldState属性は、初期化された値を返さなければならない。popover属性のポップオーバー可視性状態をもつ要素が表示の場合は"open"に初期化され、そうでなければ"closed"に初期化される。

newState属性は、初期化された値を返さなければならない。popover属性のポップオーバー可視性状態をもつ要素が表示の場合は"closed"に初期化され、そうでなければ"open"に初期化される。