pinafore/src/routes/_components/dialog/components/ModalDialog.html

268 wiersze
7.3 KiB
HTML
Czysty Zwykły widok Historia

<div class={backdropClass}
2018-03-04 00:31:00 +00:00
tabindex="-1"
data-a11y-dialog-hide
></div>
<div class={contentsClass}
2018-03-04 00:31:00 +00:00
role="dialog"
aria-label={label || ''}
2018-03-04 00:31:00 +00:00
ref:node
>
<div class="modal-dialog-document" role="document" style="background: {background || '#000'};">
<div class="modal-dialog-header" on:click="onClickHeader(event)">
{#if title}
<h1 class="modal-dialog-title">{title}</h1>
{/if}
2018-02-28 07:18:07 +00:00
<div class="close-dialog-button-wrapper">
<button class="close-dialog-button focus-fix"
data-a11y-dialog-hide aria-label="{intl.closeDialog}">
<SvgIcon className="close-dialog-button-svg" href="#fa-times" />
2018-02-28 07:18:07 +00:00
</button>
</div>
</div>
2018-02-23 04:04:19 +00:00
<slot></slot>
</div>
</div>
<Shortcut scope="modal-{id}" key="Backspace" on:pressed="closeDialog(id)"/>
<style>
2018-04-01 01:46:44 +00:00
:global(.modal-dialog[aria-hidden='true']) {
display: none;
}
:global(.modal-dialog) {
position: fixed;
z-index: 10000;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}
.modal-dialog-backdrop {
position: fixed;
z-index: 10010;
left: 0;
right: 0;
bottom: 0;
top: 0;
background: rgba(51, 51, 51, 0.9);
2018-04-01 01:46:44 +00:00
}
.modal-dialog-backdrop.should-animate {
2018-03-04 00:31:00 +00:00
transition: opacity 0.2s linear;
}
.modal-dialog-contents {
z-index: 10020;
padding: 0;
2018-02-06 17:09:47 +00:00
border: 1px solid var(--main-border);
border-radius: 2px;
2018-02-23 04:04:19 +00:00
display: flex;
flex-direction: row;
max-height: calc(100% - 20px);
max-width: calc(100% - 20px);
flex: 0 1 580px;
2018-04-01 01:46:44 +00:00
}
.modal-dialog-contents.should-animate {
2018-03-04 00:31:00 +00:00
transition: opacity 0.2s linear;
}
2018-02-23 04:04:19 +00:00
.modal-dialog-document {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
max-width: 100%;
2018-02-23 04:04:19 +00:00
flex: 1;
}
2018-02-28 07:18:07 +00:00
.modal-dialog-header {
width: 100%;
background: var(--nav-bg);
display: flex;
align-items: center;
}
.modal-dialog-title {
color: var(--nav-text-color);
padding: 2px 0 2px 10px;
margin: 0;
font-size: 1.5em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.close-dialog-button-wrapper {
2018-02-28 07:18:07 +00:00
flex: 1;
2018-03-28 00:31:17 +00:00
display: flex;
justify-content: flex-end;
}
.close-dialog-button {
2018-03-28 00:31:17 +00:00
padding: 0;
background: none;
border: none;
2018-03-28 00:31:17 +00:00
display: flex;
justify-content: center;
align-items: center;
}
:global(.close-dialog-button-svg) {
2018-03-28 15:56:35 +00:00
padding: 10px;
fill: var(--button-primary-text);
width: 24px;
height: 24px;
2018-03-28 00:31:17 +00:00
flex: 1;
}
.muted-style .modal-dialog-header {
background: var(--muted-modal-bg);
}
.muted-style .close-dialog-button:focus {
outline: 2px solid var(--muted-modal-focus);
}
.muted-style .close-dialog-button:hover {
background: var(--muted-modal-hover);
}
.muted-style.modal-dialog-contents {
border: none;
}
:global(body.modal-open) {
overflow-y: hidden;
}
@media(min-width: 480px) {
/* On desktop, some dialogs look bad if they expand to fit all the way. So we shrink
them to fit if shrinkWidthToFit is true.*/
.modal-dialog-contents.shrink-width-to-fit {
flex: none;
}
}
@media (max-width: 320px) {
.modal-dialog-title {
font-size: 1.3em;
}
:global(.close-dialog-button-svg) {
padding: 7px;
width: 18px;
height: 18px;
}
}
@media (max-width: 240px) {
.modal-dialog-contents {
min-width: calc(100% - 20px);
}
}
</style>
<script>
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 18:03:29 +00:00
import Shortcut from '../../shortcut/Shortcut.html'
import SvgIcon from '../../SvgIcon.html'
import { A11yDialog } from '../../../_thirdparty/a11y-dialog/a11y-dialog'
import { classname } from '../../../_utils/classname'
import { on, emit } from '../../../_utils/eventBus'
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 18:03:29 +00:00
import {
pushShortcutScope,
popShortcutScope
} from '../../../_utils/shortcuts'
2018-04-01 01:46:44 +00:00
export default {
2018-04-20 04:38:01 +00:00
oncreate () {
2019-08-03 20:49:37 +00:00
const { id } = this.get()
this.onPopState = this.onPopState.bind(this)
2019-08-03 20:49:37 +00:00
const dialogElement = this.refs.node.parentElement
this._a11yDialog = new A11yDialog(dialogElement)
this._a11yDialog.on('hide', () => {
document.body.classList.toggle('modal-open', false)
this.fire('close')
this._a11yDialog.destroy()
emit('destroyDialog', id)
requestAnimationFrame(() => document.body.removeChild(dialogElement))
})
on('showDialog', this, this.showDialog)
on('closeDialog', this, this.closeDialog)
pushShortcutScope(`modal-${id}`)
},
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 18:03:29 +00:00
ondestroy () {
window.removeEventListener('popstate', this.onPopState)
2019-08-03 20:49:37 +00:00
const { statePopped, statePushed, id } = this.get()
if (statePushed && !statePopped) {
// If we weren't closed due to popstate, then pop state to ensure the correct history.
window.history.back()
}
popShortcutScope(`modal-${id}`)
feat: Add support for keyboard shortcuts (#870) * Add support for keyboard shortcuts. This change introduces a Shortcut component for defining global keyboard shortcuts from whichever component makes more sense. This change also adds an initial set of navigation shortcuts: - Backspace to leave a modal dialog or to go back - g t to go to the federated timeline - g f to go to the favorite page - g h to go to the home page - g n to go to the notification page - g c to go to the community page - s to go to the search page These shortcuts are loaded asynchronously from _layout.html In modal dialogs, shortcuts are also modal, to avoid strange or overly complex behavior. This is implemented by grouping shortcuts into scopes, and activating a separate 'modal' scope when entering a modal dialog, so a separate set of shortcuts can be enabled in modal dialog. Modal dialogs can be exited by pressing 'Backspace'. * Navigate up/down lists using keyboard shortcuts. This change introduces keyboard shortcuts for navigating in lists and virtual lists. j or arrow up selects the next element, k or arrow down, the previous element. Selecting an element scrolls the list up and down, as necessary. This change also allows directing keyboard shortcuts to the active element and defines the following shortcuts, for the active status: - f to favorite or unfavorite it - b to boost or unboost it - r to reply to it - o to open its thread - x to toggle the display of a CW - y to toggle the display of sensitive medias This works by defining a keyboard shortcut scope for each list element. A new component, ScrollListShortcuts, keeps track of the active element, based on list or virtual list elements and redirects shortcuts to the active element's scope. ScrollListShortcuts keeps the active element in the current realm of the store, so the active element is restored when going back to the list. * Typing h or ? displays the list of available keyboard shortcuts. This change introduces a new modal dialog that documents the list of available shortcuts.
2019-01-13 18:03:29 +00:00
},
components: { Shortcut, SvgIcon },
2018-04-01 01:46:44 +00:00
data: () => ({
// don't animate if we're showing a modal dialog on top of another modal dialog. it looks ugly
2018-04-30 16:57:49 +00:00
shouldAnimate: !process.browser || document.getElementsByClassName('modal-dialog').length < 2,
fadedIn: false,
muted: false,
2019-08-20 02:08:59 +00:00
className: undefined,
title: undefined,
shrinkWidthToFit: false,
clickHeaderToClose: false
2018-04-01 01:46:44 +00:00
}),
computed: {
backdropClass: ({ fadedIn, shouldAnimate }) => {
2018-04-01 01:46:44 +00:00
return classname(
'modal-dialog-backdrop',
!fadedIn && 'hidden',
shouldAnimate && 'should-animate'
)
},
contentsClass: ({ fadedIn, muted, shouldAnimate, shrinkWidthToFit, className }) => {
2018-04-01 01:46:44 +00:00
return classname(
'modal-dialog-contents',
!fadedIn && 'hidden',
muted && 'muted-style',
shouldAnimate && 'should-animate',
shrinkWidthToFit && 'shrink-width-to-fit',
2018-04-01 01:46:44 +00:00
className
)
}
},
methods: {
showDialog (otherId) {
2019-08-03 20:49:37 +00:00
const { id } = this.get()
if (otherId !== id) {
return
2018-02-05 17:43:45 +00:00
}
// This setTimeout is dumb, but it fixes issues with modals opening other modals
// due to the popstate/pushstate dance.
setTimeout(() => {
requestAnimationFrame(() => {
window.addEventListener('popstate', this.onPopState)
this.set({ statePushed: true })
window.history.pushState({ modal: id }, null, location.href)
document.body.classList.toggle('modal-open', true)
this._a11yDialog.show()
this.set({ fadedIn: true })
this.fire('show')
emit('dialogDidRender', id)
})
})
},
onPopState (event) {
2019-08-03 20:49:37 +00:00
const { id } = this.get()
if (!(event.state && event.state.modal === id)) {
// If the new state is not us, just assume that we need to be closed.
// This will only fail if modals are ever nested more than 2 levels deep.
this.set({ statePopped: true })
this.closeDialog(id)
}
},
closeDialog (otherId) {
2019-08-03 20:49:37 +00:00
const { id } = this.get()
if (id !== otherId) {
return
}
this._a11yDialog.hide()
},
onClickHeader (e) {
if (this.get().clickHeaderToClose) {
e.preventDefault()
e.stopPropagation()
this._a11yDialog.hide()
}
}
}
}
</script>