-
Notifications
You must be signed in to change notification settings - Fork 42
Modal for links #389
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
SharonStrats
wants to merge
13
commits into
main
Choose a base branch
from
modal-for-links
base: main
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
Modal for links #389
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eefe66e
added modals
SharonStrats 5e2a431
added styles for modal
SharonStrats f947e1a
exported modals
SharonStrats ccbd029
merged main
SharonStrats e05ffe1
changed style to new way
SharonStrats e5b4110
avoid doc-wide .modal lookup
SharonStrats 1b682fc
improve name and return modal
SharonStrats 6a798d8
Apply suggestions from code review
SharonStrats fabb939
update
SharonStrats da4fe11
lint fix
SharonStrats 7eb175b
accessibility
SharonStrats 9426f6c
tests
SharonStrats a99b5ff
added stories
SharonStrats 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
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,55 @@ | ||
| import * as UI from '../../src/index' | ||
|
|
||
| export default { | ||
| title: 'Modals', | ||
| } | ||
|
|
||
| const demoLinks = [ | ||
| { label: 'Solid Project', link: 'https://solidproject.org/' }, | ||
| { label: 'SolidOS', link: 'https://github.com/solidos' }, | ||
| { label: 'W3C', link: 'https://www.w3.org/' }, | ||
| ] | ||
|
|
||
| export const ListModal = { | ||
| render: () => { | ||
| const modal = UI.widgets.createListModal(document, demoLinks, { | ||
| withGreyedBackground: true, | ||
| ariaLabel: 'Helpful links', | ||
| }) | ||
| modal.style.display = 'block' | ||
| return modal | ||
| }, | ||
| name: 'List modal', | ||
| } | ||
|
|
||
| export const OpenListModal = { | ||
| render: () => { | ||
| const container = document.createElement('div') | ||
|
|
||
| const openButton = document.createElement('button') | ||
| openButton.setAttribute('type', 'button') | ||
| openButton.textContent = 'Open links modal' | ||
|
|
||
| const helper = document.createElement('p') | ||
| helper.textContent = 'Open the modal, then press Escape or use the close button.' | ||
|
|
||
| openButton.addEventListener('click', () => { | ||
| const existing = container.querySelector('.modal') | ||
| if (existing) { | ||
| existing.remove() | ||
| } | ||
|
|
||
| const modal = UI.widgets.createListModal(document, demoLinks, { | ||
| withGreyedBackground: true, | ||
| ariaLabel: 'Helpful links', | ||
| }) | ||
| modal.style.display = 'block' | ||
| container.appendChild(modal) | ||
| }) | ||
|
|
||
| container.appendChild(openButton) | ||
| container.appendChild(helper) | ||
| return container | ||
| }, | ||
| name: 'Open list modal', | ||
| } |
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,116 @@ | ||
| import { | ||
| ModalWidgetStyleOptions, | ||
| getModalStyle | ||
| } from './modalsStyle' | ||
| import { style } from '../style' | ||
|
|
||
| export type ListItem = { | ||
| label: string, | ||
| link: string | ||
| } | ||
|
|
||
| // Two functions that need to be implemented to use the modal. | ||
| // When the user clicks the button, open the modal. | ||
| /* Click handler on the button to display it. | ||
| btn.onclick = function() { | ||
| modal.style.display = "block"; | ||
| } | ||
|
|
||
| // When the user clicks anywhere outside of the modal, close it. | ||
| // Window click handler so that the modal will close | ||
| // even if the user doesn't click close. | ||
| window.onclick = function(event) { | ||
| if (event.target == modal) { | ||
| modal.style.display = "none"; | ||
| } */ | ||
| const closeClickHandler = (modal: HTMLDivElement, returnFocusTo?: Element | null) => { | ||
| modal.style.display = 'none' | ||
| if (returnFocusTo && returnFocusTo instanceof HTMLElement) { | ||
| returnFocusTo.focus() | ||
| } | ||
| } | ||
|
|
||
| const createModal = (dom: HTMLDocument, options: ModalWidgetStyleOptions, returnFocusTo?: Element | null) => { | ||
| const modal = dom.createElement('div') | ||
| modal.classList.add('modal') | ||
| modal.setAttribute('role', 'dialog') | ||
| modal.setAttribute('aria-modal', options.withGreyedBackground ? 'true' : 'false') | ||
| modal.setAttribute('aria-label', options.ariaLabel || 'List dialog') | ||
| modal.setAttribute('tabindex', '-1') | ||
| modal.addEventListener('keydown', event => { | ||
| if (event.key === 'Escape') { | ||
| closeClickHandler(modal, returnFocusTo) | ||
| } | ||
| }) | ||
| modal.setAttribute('style', getModalStyle(options)) | ||
| return modal | ||
| } | ||
|
|
||
| const createModalContent = (dom: HTMLDocument) => { | ||
| const modalContent: HTMLDivElement = dom.createElement('div') | ||
| modalContent.setAttribute('style', style.modalContentStyle) | ||
| return modalContent | ||
| } | ||
|
|
||
| const createCloseButton = (dom: HTMLDocument, modal: HTMLDivElement, returnFocusTo?: Element | null) => { | ||
| const closeButton: HTMLButtonElement = dom.createElement('button') | ||
| closeButton.setAttribute('type', 'button') | ||
| closeButton.setAttribute('aria-label', 'Close modal') | ||
| closeButton.setAttribute('title', 'Close') | ||
| closeButton.textContent = 'x' | ||
| closeButton.addEventListener('click', () => closeClickHandler(modal, returnFocusTo)) | ||
| closeButton.addEventListener('mouseenter', () => { | ||
| closeButton.setAttribute('style', style.modalCloseStyleHover) | ||
| }) | ||
| closeButton.addEventListener('mouseleave', () => { | ||
| closeButton.setAttribute('style', style.modalCloseStyle) | ||
| }) | ||
| closeButton.addEventListener('focus', () => { | ||
| closeButton.setAttribute('style', style.modalCloseStyleFocus) | ||
| }) | ||
| closeButton.addEventListener('blur', () => { | ||
| closeButton.setAttribute('style', style.modalCloseStyle) | ||
| }) | ||
| closeButton.setAttribute('style', style.modalCloseStyle) | ||
| return closeButton | ||
| } | ||
|
|
||
| const createListItems = (dom: HTMLDocument, list: ListItem) => { | ||
| const li:HTMLLIElement = dom.createElement('li') | ||
| li.setAttribute('style', style.modalListItemStyle) | ||
| const link: HTMLAnchorElement = dom.createElement('a') | ||
| link.setAttribute('style', style.modalAnchorStyle) | ||
| link.href = list.link | ||
| link.textContent = list.label | ||
| li.appendChild(link) | ||
| return li | ||
| } | ||
|
|
||
| const createUnorderedList = (dom: HTMLDocument, listOfLinks: ListItem[]) => { | ||
| const ul: HTMLUListElement = dom.createElement('ul') | ||
| ul.setAttribute('role', 'list') | ||
| ul.setAttribute('style', style.modalUnorderedListStyle) | ||
| listOfLinks.forEach(list => { | ||
| const li = createListItems(dom, list) | ||
| ul.appendChild(li) | ||
| }) | ||
| return ul | ||
| } | ||
| export const createListModal = (dom: HTMLDocument, listOfLinks: ListItem[], options: ModalWidgetStyleOptions) => { | ||
| const returnFocusTo = dom.activeElement | ||
| const modal = createModal(dom, options, returnFocusTo) | ||
| const modalContent = createModalContent(dom) | ||
| const closeButton = createCloseButton(dom, modal, returnFocusTo) | ||
| const ul = createUnorderedList(dom, listOfLinks) | ||
| modalContent.appendChild(closeButton) | ||
| modalContent.appendChild(ul) | ||
| modal.appendChild(modalContent) | ||
|
|
||
| setTimeout(() => { | ||
| if (closeButton.isConnected) { | ||
| closeButton.focus() | ||
| } | ||
| }, 0) | ||
|
|
||
| return modal | ||
| } | ||
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,21 @@ | ||
| /** | ||
| * Get the modal styles, based on options such as position and background overlay. | ||
| * See https://design.inrupt.com/atomic-core/?cat=Organisms#Modals for modal design guidelines. | ||
| */ | ||
| export type ModalWidgetStyleOptions = { | ||
| topPosition?: string, | ||
| leftPosition?: string, | ||
| withGreyedBackground?: boolean, | ||
| ariaLabel?: string | ||
| } | ||
|
|
||
| export const getModalStyle = (options: ModalWidgetStyleOptions = {}) => { | ||
| const topPosition = (options.topPosition) ? options.topPosition : '50px' | ||
| const leftPosition = (options.leftPosition) ? options.leftPosition : '50px' | ||
|
|
||
| if (options.withGreyedBackground) { | ||
| return 'display: none; position: fixed; z-index: 1; overflow: auto; background-color: rgba(0,0,0,0.4); padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%;' | ||
| } | ||
|
|
||
| return `display: none; position: fixed; z-index: 1; top: ${topPosition}; left: ${leftPosition}; overflow: auto; background-color: rgba(0,0,0,0.4);` | ||
| } |
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.
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.