-
Notifications
You must be signed in to change notification settings - Fork 275
refactor(frontend): remove spicy-sections #4572
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
Open
ashmod
wants to merge
9
commits into
google:master
Choose a base branch
from
ashmod:refactor/remove-spicy-sections
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+423
−236
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12af628
unspiced sections
ashmod e7a7851
fix collapse/expand logic
ashmod c1952fc
fix ecosystem filters
ashmod 865ef3e
styling
ashmod 7b2be0a
refactor tab layout
ashmod 14e8319
rename
ashmod ce28d5f
use mixins and update paddings
ashmod 415a377
Merge branch 'master' into refactor/remove-spicy-sections
ashmod 613d27d
remove redundant check
ashmod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| class OsvTabs extends HTMLElement { | ||
| constructor() { | ||
| super(); | ||
| this.breakpoint = 500; | ||
| this.mediaQuery = null; | ||
| this.headers = []; | ||
| this.panels = []; | ||
| this.activeIndex = 0; | ||
| this.headerListeners = null; | ||
|
|
||
| // shadow DOM for tab-bar layout | ||
| this.attachShadow({ mode: "open" }); | ||
| this.shadowRoot.innerHTML = ` | ||
| <style> | ||
| :host([affordance="tab-bar"]) { | ||
| display: block; | ||
| } | ||
| :host([affordance="tab-bar"]) .tab-list { | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| } | ||
| :host([affordance="tab-bar"]) ::slotted(*) { | ||
| display: none; | ||
| } | ||
| :host([affordance="tab-bar"]) .tab-list ::slotted(h2), | ||
| :host([affordance="tab-bar"]) .tab-list ::slotted(h3) { | ||
| display: block; | ||
| } | ||
| :host([affordance="tab-bar"]) .panel-container ::slotted(div[data-panel-active]) { | ||
| display: block; | ||
| } | ||
| :host([affordance="collapse"]) .tab-list { | ||
| display: none; | ||
| } | ||
| :host([affordance="collapse"]) .panel-container { | ||
| display: none; | ||
| } | ||
| :host([affordance="collapse"]) ::slotted(*) { | ||
| display: block; | ||
| } | ||
| </style> | ||
| <div class="tab-list" part="tab-list"> | ||
| <slot name="tab"></slot> | ||
| </div> | ||
| <div class="panel-container" part="panel-container"> | ||
| <slot name="panel"></slot> | ||
| </div> | ||
| <slot></slot> | ||
| `; | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| this.breakpoint = parseInt(this.getAttribute("breakpoint")) || 500; | ||
| this.collectHeadersAndPanels(); | ||
| this.setupMediaQuery(); | ||
| this.updateAffordance(); | ||
| this.setupEventListeners(); | ||
| } | ||
|
|
||
| disconnectedCallback() { | ||
| if (this.mediaQuery) { | ||
| this.mediaQuery.removeEventListener("change", this.boundUpdateAffordance); | ||
| } | ||
| this.removeEventListeners(); | ||
| } | ||
|
|
||
| collectHeadersAndPanels() { | ||
| this.headers = []; | ||
| this.panels = []; | ||
|
|
||
| const children = Array.from(this.children); | ||
| for (let i = 0; i < children.length; i++) { | ||
| const child = children[i]; | ||
| if (child.matches("h2, h3")) { | ||
| const nextSibling = children[i + 1]; | ||
| if (nextSibling && nextSibling.matches("div")) { | ||
| this.headers.push(child); | ||
| this.panels.push(nextSibling); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| setupMediaQuery() { | ||
| this.mediaQuery = window.matchMedia( | ||
| `(min-width: ${this.breakpoint + 1}px)` | ||
| ); | ||
| this.boundUpdateAffordance = () => this.updateAffordance(); | ||
| this.mediaQuery.addEventListener("change", this.boundUpdateAffordance); | ||
| } | ||
|
|
||
| setupEventListeners() { | ||
| this.headerListeners = this.headers.map((header, index) => { | ||
| const clickListener = (e) => this.handleHeaderClick(index, e); | ||
| const keydownListener = (e) => this.handleKeydown(index, e); | ||
|
|
||
| header.addEventListener("click", clickListener); | ||
| header.addEventListener("keydown", keydownListener); | ||
|
|
||
| return { header, clickListener, keydownListener }; | ||
| }); | ||
| } | ||
|
|
||
| removeEventListeners() { | ||
| if (this.headerListeners) { | ||
| this.headerListeners.forEach(({ header, clickListener, keydownListener }) => { | ||
| header.removeEventListener("click", clickListener); | ||
| header.removeEventListener("keydown", keydownListener); | ||
| }); | ||
| this.headerListeners = null; | ||
| } | ||
| } | ||
|
|
||
| updateAffordance() { | ||
| const isDesktop = this.mediaQuery.matches; | ||
| const affordance = isDesktop ? "tab-bar" : "collapse"; | ||
| this.setAttribute("affordance", affordance); | ||
|
|
||
| if (isDesktop) { | ||
| this.renderTabs(); | ||
| } else { | ||
| this.renderAccordion(); | ||
| } | ||
| } | ||
|
|
||
| renderTabs() { | ||
| this.headers.forEach((header, index) => { | ||
| const isActive = index === this.activeIndex; | ||
| header.setAttribute("slot", "tab"); | ||
| header.setAttribute("tabindex", isActive ? "0" : "-1"); | ||
| header.setAttribute("role", "tab"); | ||
| header.setAttribute("aria-selected", isActive ? "true" : "false"); | ||
| header.removeAttribute("aria-expanded"); | ||
| header.removeAttribute("expanded"); | ||
| }); | ||
|
|
||
| this.panels.forEach((panel, index) => { | ||
| const isActive = index === this.activeIndex; | ||
| panel.setAttribute("slot", "panel"); | ||
| panel.setAttribute("role", "tabpanel"); | ||
| if (isActive) { | ||
| panel.setAttribute("data-panel-active", ""); | ||
| } else { | ||
| panel.removeAttribute("data-panel-active"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| renderAccordion() { | ||
| this.headers.forEach((header, index) => { | ||
| const panel = this.panels[index]; | ||
|
|
||
| header.removeAttribute("slot"); | ||
| header.removeAttribute("role"); | ||
| header.removeAttribute("aria-selected"); | ||
| header.setAttribute("tabindex", "0"); | ||
| header.setAttribute("aria-expanded", "true"); | ||
| header.setAttribute("expanded", ""); | ||
|
|
||
| panel.removeAttribute("slot"); | ||
| panel.removeAttribute("data-panel-active"); | ||
| panel.style.display = ""; | ||
| }); | ||
|
|
||
| this.panels.forEach((panel) => { | ||
| panel.setAttribute("role", "region"); | ||
| }); | ||
| } | ||
|
|
||
| handleHeaderClick(index, event) { | ||
| if (!this.headers[index].contains(event.target)) { | ||
| return; | ||
| } | ||
|
|
||
| const affordance = this.getAttribute("affordance"); | ||
|
|
||
| if (affordance === "tab-bar") { | ||
| this.activeIndex = index; | ||
| this.renderTabs(); | ||
| } else { | ||
| const panel = this.panels[index]; | ||
| const header = this.headers[index]; | ||
| const isExpanded = panel.style.display !== "none"; | ||
|
|
||
| panel.style.display = isExpanded ? "none" : ""; | ||
| header.setAttribute("aria-expanded", isExpanded ? "false" : "true"); | ||
| if (isExpanded) { | ||
| header.removeAttribute("expanded"); | ||
| } else { | ||
| header.setAttribute("expanded", ""); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| handleKeydown(index, event) { | ||
| const affordance = this.getAttribute("affordance"); | ||
|
|
||
| if (event.key === "Enter" || event.key === " ") { | ||
| event.preventDefault(); | ||
| this.handleHeaderClick(index, { target: this.headers[index] }); | ||
| } | ||
|
|
||
| if (affordance === "tab-bar") { | ||
| if (event.key === "ArrowRight" || event.key === "ArrowDown") { | ||
| event.preventDefault(); | ||
| const nextIndex = (index + 1) % this.headers.length; | ||
| this.activeIndex = nextIndex; | ||
| this.renderTabs(); | ||
| this.headers[nextIndex].focus(); | ||
| } else if (event.key === "ArrowLeft" || event.key === "ArrowUp") { | ||
| event.preventDefault(); | ||
| const prevIndex = | ||
| (index - 1 + this.headers.length) % this.headers.length; | ||
| this.activeIndex = prevIndex; | ||
| this.renderTabs(); | ||
| this.headers[prevIndex].focus(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| customElements.define("osv-tabs", OsvTabs); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.