-
Notifications
You must be signed in to change notification settings - Fork 11
feat(unity-react-core): extend tooltip to own component #1677
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
scott-williams-az
wants to merge
2
commits into
dev
Choose a base branch
from
uds-1649
base: dev
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.
Open
Changes from all commits
Commits
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
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,97 @@ | ||
| import { EventHandler } from "./bootstrap-helper"; | ||
|
|
||
| function initTooltips() { | ||
| /* this value must stay in sync, found in files: */ | ||
| /* packages/unity-bootstrap-theme/src/scss/extends/_tooltips.scss */ | ||
| /* packages/unity-bootstrap-theme/src/js/tooltips.js */ | ||
| const TOOLTIP_MAX_WIDTH = 288; | ||
| const CONTAINER_CLASS = "uds-tooltip-container"; | ||
| const CONTAINER_SELECTOR = `.${CONTAINER_CLASS}`; | ||
| const TRIGGER_ATTR = "aria-describedby"; | ||
| const TRIGGER_SELECTOR = `[${TRIGGER_ATTR}]`; | ||
| const CONTENT_ATTR = "role=tooltip"; | ||
| const CONTENT_SELECTOR = `[${CONTENT_ATTR}]`; | ||
|
|
||
| // This query selector is not just creating a List, | ||
| // it's also checking to ensure all 3 elements are present | ||
| // (container, trigger, content) and in the correct order | ||
| // (trigger immediately followed by content) | ||
| const tooltipContentList = document.querySelectorAll( | ||
| `${CONTAINER_SELECTOR} > ${TRIGGER_SELECTOR} + ${CONTENT_SELECTOR}` | ||
| ); | ||
|
|
||
| function closeActiveTooltips() { | ||
| const activeTooltips = document.querySelectorAll( | ||
| `${TRIGGER_SELECTOR}[aria-expanded="true"]` | ||
| ); | ||
| activeTooltips.forEach(activeTooltip => { | ||
| activeTooltip.setAttribute("aria-expanded", "false"); | ||
| }); | ||
| } | ||
|
|
||
| function show(e) { | ||
| // container or trigger | ||
| let trigger = | ||
| e.target.querySelector(`${CONTAINER_SELECTOR} ${TRIGGER_SELECTOR}`) || | ||
| e.target; | ||
| let content = trigger.nextSibling; | ||
|
|
||
| if (e.type === "keypress") { | ||
| if (e.charCode !== 32) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use e.key since keypress and charCode is deprecated |
||
| return; | ||
| } | ||
| } | ||
|
|
||
| closeActiveTooltips(); | ||
|
|
||
| if ( | ||
| trigger.getBoundingClientRect().right + TOOLTIP_MAX_WIDTH > | ||
| window.innerWidth | ||
| ) { | ||
| content.classList.add("bottom-placement"); | ||
| } else { | ||
| content.classList.remove("bottom-placement"); | ||
| } | ||
| trigger.setAttribute("aria-expanded", "true"); | ||
| } | ||
|
|
||
| function hide(e) { | ||
| // container or trigger | ||
| let trigger = | ||
| e.target.querySelector(`${CONTAINER_SELECTOR} ${TRIGGER_SELECTOR}`) || | ||
| e.target; | ||
|
|
||
| if (e.type === "mouseleave") { | ||
| if (trigger === document.activeElement) { | ||
| return; | ||
| } | ||
| } | ||
| trigger.setAttribute("aria-expanded", "false"); | ||
| } | ||
|
|
||
| function keyboardHide(e) { | ||
| if (e.key === "Escape") { | ||
| hide(e); | ||
| } | ||
| } | ||
|
|
||
| [...tooltipContentList].map(contentEl => { | ||
| const controller = new AbortController(); | ||
| const { signal } = controller; | ||
| const triggerEl = contentEl.previousElementSibling; | ||
| const containerEl = triggerEl.parentElement; | ||
|
|
||
| triggerEl.addEventListener("mouseenter", show, { signal }); | ||
| triggerEl.addEventListener("focus", show, { signal }); | ||
| triggerEl.addEventListener("keypress", show, { signal }); | ||
| triggerEl.addEventListener("blur", hide, { signal }); | ||
| triggerEl.addEventListener("keydown", keyboardHide, { signal }); | ||
| containerEl.addEventListener("mouseleave", hide, { signal }); | ||
|
|
||
| return controller; | ||
| }); | ||
| } | ||
|
|
||
| EventHandler.on(window, "load.uds.tooltips", initTooltips); | ||
|
|
||
| export { initTooltips }; | ||
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 |
|---|---|---|
| @@ -1,32 +1,43 @@ | ||
| @mixin focusState { | ||
| + div[role='tooltip'].uds-tooltip-description { | ||
| visibility: visible; | ||
| } | ||
| .fa-circle { | ||
| color: $uds-color-font-light-info; | ||
| } | ||
| } | ||
|
|
||
| .uds-tooltip-container { | ||
| /* this value must stay in sync, found in files: */ | ||
| /* packages/unity-bootstrap-theme/src/scss/extends/_tooltips.scss */ | ||
| /* packages/unity-bootstrap-theme/src/js/tooltips.js */ | ||
| --tooltip-max-width: 288px; | ||
|
|
||
| --tooltip-offset: .5rem; | ||
| display: inline-block; | ||
| position: relative; | ||
|
|
||
| &::before { | ||
| content: ''; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| right: calc(-1 * var(--tooltip-offset)); | ||
| bottom: calc(-1 * var(--tooltip-offset)); | ||
| } | ||
|
|
||
| [aria-describedby] { | ||
| & { | ||
| position: relative; | ||
| } | ||
|
|
||
| + [role="tooltip"] { | ||
| visibility: hidden; | ||
| display: none; | ||
| z-index: 2; | ||
| } | ||
| } | ||
|
|
||
| [aria-describedby]:focus, | ||
| [aria-describedby]:hover { | ||
| [aria-describedby][aria-expanded='true'] { | ||
| + [role="tooltip"] { | ||
| visibility: visible; | ||
| display: block; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| button.uds-tooltip { | ||
| background: none; | ||
| background-color: transparent; | ||
| color: inherit; | ||
| border: none; | ||
| padding: 0; | ||
|
|
@@ -50,12 +61,9 @@ button.uds-tooltip { | |
| vertical-align: middle; | ||
| } | ||
|
|
||
| &:focus { | ||
| @include focusState(); | ||
| } | ||
| @include media-breakpoint-up(sm) { | ||
| &:hover { | ||
| @include focusState(); | ||
| &[aria-expanded='true'] { | ||
| .fa-circle { | ||
| color: $uds-color-font-light-info; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -95,16 +103,23 @@ div[role='tooltip'].uds-tooltip-description { | |
| color: $asu-gray-7; | ||
| font: normal normal normal $uds-size-spacing-2 Arial; | ||
| line-height: $uds-size-spacing-3; | ||
| margin: 0px 5px; | ||
| max-width: 353px; | ||
| min-width: 300px; | ||
| max-width: var(--tooltip-max-width); | ||
| min-width: min(100vw, var(--min-width)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see --min-width defined up top |
||
| width: -webkit-max-content; | ||
| padding: $uds-size-spacing-4; | ||
| position: absolute; | ||
| left: 40px; | ||
| left: calc(100% + var(--tooltip-offset)); | ||
| top: 0; | ||
| visibility: hidden; | ||
| justify-self: start; | ||
| align-self: end; | ||
| z-index: 1; | ||
|
|
||
| &.bottom-placement { | ||
| left: unset; | ||
| top: calc(100% + var(--tooltip-offset)); | ||
| right: 0; | ||
| } | ||
|
|
||
| & > span.uds-tooltip-heading { | ||
| color: $asu-gray-7; | ||
| display: block; | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think nextElementSibling might be better here just in case nextSibling picks up any whitespace elements or line breaks