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

fix(core/dfn-panel): avoid using CSS variables #2888

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions assets/dfn-panel.css
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
/* dfn popup panel that list all local references to a dfn */
/**
* TODO: Revert changes due to https://github.com/w3c/respec/pull/2888 when
* https://github.com/w3c/css-validator/pull/111 is fixed.
*/
dfn {
cursor: pointer;
}

.dfn-panel {
--fill: #fff;
position: absolute;
left: var(--left); /* set via JS */
top: var(--top); /* set via JS */
z-index: 35;
min-width: 300px;
max-width: 500px;
padding: 0.5em 0.75em;
margin-top: 0.6em;
font: small Helvetica Neue, sans-serif, Droid Sans Fallback;
background: var(--fill);
background: #fff;
color: black;
box-shadow: 0 1em 3em -0.4em rgba(0, 0, 0, 0.3),
0 0 1px 1px rgba(0, 0, 0, 0.05);
border-radius: 2px;
}

/* Triangle/caret */
.dfn-panel:not(.docked)::before,
.dfn-panel:not(.docked)::after {
.dfn-panel:not(.docked) > .caret {
position: relative;
top: -0.25em;
}
.dfn-panel:not(.docked) > .caret::before,
.dfn-panel:not(.docked) > .caret::after {
content: "";
position: absolute;
border: 10px solid transparent;
border-top: 0;
border-bottom: 9px solid #a2a9b1; /* triangle outline */
top: -9px;
left: calc(var(--caret-offset, 0px) + 0.75em); /* set via JS */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be better to use attr() in the future rather than just reverting this. Google just posted an intent to implement for the feature. https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/FGCgsKmylhw/BSUrk2roCQAJ

Since the validator is quite inactive, the browser implementation might be quicker.

Copy link
Collaborator

@saschanaz saschanaz May 20, 2020

Choose a reason for hiding this comment

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

Looking at the code, the validator already supports attr() so we probably can just use it when the browser support lands.

Copy link
Member Author

@sidvishnoi sidvishnoi May 20, 2020

Choose a reason for hiding this comment

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

Even though nuHTML allows advanced attr(), CSS validator doesn't yet.

This comment was marked as outdated.

Copy link
Collaborator

@saschanaz saschanaz May 20, 2020

Choose a reason for hiding this comment

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

Hey, actually it does validate it!

.abc {
  width: calc(attr(data-foo px) / 10);
}

Try this code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right! I missed the px suffix

border-bottom: 10px solid #fff;
top: 0;
}
.dfn-panel:not(.docked)::after {
border-bottom: 10px solid var(--fill); /* triangle fill */
.dfn-panel:not(.docked) > .caret::before {
border-bottom: 10px solid #a2a9b1;
}

.dfn-panel * {
Expand Down
11 changes: 7 additions & 4 deletions src/core/dfn-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export async function run() {
break;
}
case "dock": {
panel.style.left = null;
panel.style.top = null;
panel.classList.add("docked");
break;
}
Expand Down Expand Up @@ -70,6 +72,7 @@ function createPanel(dfn) {
/** @type {HTMLElement} */
const panel = hyperHTML`
<aside class="dfn-panel" id="dfn-panel">
<span class="caret"></span>
<b><a class="self-link" href="${href}">Permalink</a></b>
<b>Referenced in:</b>
${referencesToHTML(id, links)}
Expand Down Expand Up @@ -158,17 +161,17 @@ function displayPanel(dfn, panel, { x, y }) {

const top = window.scrollY + closestTop + dfnRects[0].height;
const left = x - MARGIN;
panel.style.setProperty("--left", `${left}px`);
panel.style.setProperty("--top", `${top}px`);
panel.style.left = `${left}px`;
panel.style.top = `${top}px`;

// Find if the panel is flowing out of the window
const panelRect = panel.getBoundingClientRect();
const SCREEN_WIDTH = Math.min(window.innerWidth, window.screen.width);
if (panelRect.right > SCREEN_WIDTH) {
const newLeft = Math.max(MARGIN, x + MARGIN - panelRect.width);
const newCaretOffset = left - newLeft;
panel.style.setProperty("--left", `${newLeft}px`);
panel.style.setProperty("--caret-offset", `${newCaretOffset}px`);
panel.style.left = `${newLeft}px`;
panel.querySelector(".caret").style.left = `${newCaretOffset}px`;
}
}

Expand Down