Skip to content

Commit

Permalink
Add popover=hint example (#295)
Browse files Browse the repository at this point in the history
* Add popover=hint example

* Update tooltip wording

* correct event listener

* Add source options and remove CSS anchor references; take advantage of implicit anchor references

* Add help paragraph

* another tweak to the help box

* Update README.md

---------

Co-authored-by: Brian Smith <[email protected]>
  • Loading branch information
chrisdavidmills and bsmth authored Feb 13, 2025
1 parent 8884ad4 commit 773e0af
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Code examples that accompany various MDN DOM and Web API documentation pages.

- The "pointer-lock" directory contains a basic demo to show usage of the Pointer Lock API. You can find more explanation of how the demo works at the main MDN [Pointer Lock API](https://developer.mozilla.org/docs/Web/API/Pointer_Lock_API) page. [Run the demo live](https://mdn.github.io/dom-examples/pointer-lock/).

- The "popover-api" directory is for examples and demos of the [Popover API](https://developer.mozilla.org/docs/Web/API/Popover_API) standard. Go to the [Popover API demo index](popover-api/) to see what's available.
- The "popover-api" directory is for examples and demos of the [Popover API](https://developer.mozilla.org/docs/Web/API/Popover_API) standard. Go to the [Popover API demo index](https://mdn.github.io/dom-examples/popover-api/) to see what's available.

- The "reporting-api" directory contains a couple of basic demos to show usage of the Reporting API. You can find more explanation of how the API works in the main MDN [Reporting API](https://developer.mozilla.org/docs/Web/API/Reporting_API) docs. [Run the deprecation report demo live](https://mdn.github.io/dom-examples/reporting-api/deprecation_report.html).

Expand Down
24 changes: 16 additions & 8 deletions popover-api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ <h1>MDN Popover API examples</h1>
Demonstrates a basic auto state popover.
</li>
<li>
<a href="blur-background/">Blur background popover example</a>: Shows how
you can add styling to the content behind the popover using the
<a href="blur-background/">Blur background popover example</a>: Shows
how you can add styling to the content behind the popover using the
<code>::backdrop</code> pseudo-element.
</li>
<li>
<a href="multiple-auto/">Multiple auto popovers example</a>: Demonstrates
that, generally, only one auto popover can be displayed at once.
<a href="multiple-auto/">Multiple auto popovers example</a>:
Demonstrates that, generally, only one auto popover can be displayed at
once.
</li>
<li>
<a href="multiple-manual/">Multiple manual popovers example</a>:
Expand All @@ -41,14 +42,21 @@ <h1>MDN Popover API examples</h1>
<a href="nested-popovers/">Nested popover menu example</a>: Demonstrates
the behavior of nested auto state popovers.
</li>
<li>
<a href="popover-hint/">popover="hint" example</a>: This example shows
how to use the <code>popover="hint"</code> value to create tooltip
popovers that show on button mouseover and focus, without dismissing the
<code>auto</code> popovers shown when the buttons are clicked on. The
<code>hint</code> popovers are controlled via JavaScript.
</li>
<li>
<a href="popover-positioning/">Popover positioning example</a>: An
isolated example showing CSS overriding of the default popover positioning
specified by the UA sylesheet.
isolated example showing CSS overriding of the default popover
positioning specified by the UA sylesheet.
</li>
<li>
<a href="toggle-help-ui/">Toggle help UI example</a>: Shows the basics of
using JavaScript to control popover showing and hiding.
<a href="toggle-help-ui/">Toggle help UI example</a>: Shows the basics
of using JavaScript to control popover showing and hiding.
</li>
<li>
<a href="toast-popovers/">Toast popovers example</a>: Illustrates how to
Expand Down
136 changes: 136 additions & 0 deletions popover-api/popover-hint/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* General styling */

* {
box-sizing: border-box;
}

html {
font-family: sans-serif;
height: 100%;
overflow: hidden;
}

body {
height: inherit;
background: #ccc;
margin: 0;
}

#wrapper {
height: inherit;
display: flex;
justify-content: center;
align-items: center;
}

/* Button bar styling */

#button-bar {
height: 48px;
width: 30%;
min-width: 300px;
padding: 8px;
border-radius: 6px;
background: #eee;
box-shadow: 1px 1px 3px #999;
display: flex;
justify-content: space-around;
gap: 10px;
}

/* Button styling */

#button-bar button {
border: 1px solid #999;
border-radius: 4px;
background: #eee;
flex: 1;
}

#button-bar button:hover,
#button-bar button:focus {
background: #ccc;
}

#button-bar button:active {
box-shadow: inset 1px 1px 2px #333, inset -1px -1px 2px #eee;
}

/* Styling auto popovers */

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

width: 200px;
text-align: center;
padding: 8px;
border-radius: 6px;
background: #eee;
box-shadow: 1px 1px 3px #999;
border: none;
}

[popover="auto"] button {
border: 1px solid #999;
border-radius: 4px;
background: #eee;
width: 100%;
line-height: 1.5;
}

[popover="auto"] button:first-of-type {
margin-bottom: 8px;
}

[popover="auto"] button:hover,
[popover="auto"] button:focus {
background: #ccc;
}

[popover="auto"] button:active {
box-shadow: inset 1px 1px 2px #333, inset -1px -1px 2px #eee;
}

/* Styling hint popovers */

[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;
}

/* Styling help para */

.help-para {
position: absolute;
top: 16px;
left: 16px;
background: #eee;
font-size: 0.8rem;
line-height: 1.3;
width: 50%;
max-width: 600px;
margin: 0;
padding: 16px;
box-shadow: 1px 1px 3px #999;
}

@media (max-width: 640px) {
.help-para {
width: auto;
right: 16px;
}
}
62 changes: 62 additions & 0 deletions popover-api/popover-hint/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Popover hint example</title>
<link href="index.css" rel="stylesheet" />
<script src="index.js" defer></script>
</head>
<body>
<div id="wrapper">
<section id="button-bar">
<button
popovertarget="submenu-1"
popovertargetaction="toggle"
id="menu-1">
Menu A
</button>

<button
popovertarget="submenu-2"
popovertargetaction="toggle"
id="menu-2">
Menu B
</button>

<button
popovertarget="submenu-3"
popovertargetaction="toggle"
id="menu-3">
Menu C
</button>
</section>
</div>

<p class="help-para">
Press the buttons to show the auto popovers. Hover or focus the buttons to
show the hint popovers. When an auto popover is shown, you can show the
hint popovers without hiding it. See
<a
href="https://developer.mozilla.org/en-US/docs/Web/API/Popover_API/Using#using_hint_popover_state"
>Using "hint" popover state</a
>
for more information. Note that in non-supporting browsers, the position
of the popovers is broken.
</p>

<div id="submenu-1" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>
<div id="submenu-2" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>
<div id="submenu-3" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>

<div id="tooltip-1" class="tooltip" popover="hint">Tooltip A</div>
<div id="tooltip-2" class="tooltip" popover="hint">Tooltip B</div>
<div id="tooltip-3" class="tooltip" popover="hint">Tooltip C</div>
</body>
</html>
24 changes: 24 additions & 0 deletions popover-api/popover-hint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const tooltips = document.querySelectorAll(".tooltip");
const btns = document.querySelectorAll("#button-bar button");

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

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

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

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

for (let i = 0; i < btns.length; i++) {
addEventListeners(i);
}

0 comments on commit 773e0af

Please sign in to comment.