Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | 2x 60x 44x 60x 60x 104x 44x 2x 44x 60x 60x 44x 44x 132x 104x 104x 214x 214x 214x 214x 104x 104x 60x 60x 50x 44x 44x 6x 6x 6x 110x 214x 44x 44x 2x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 1x 1x 23x 24x 21x 21x 21x 21x 21x 21x 21x 21x 20x 21x 21x 20x 21x 21x 21x 21x 21x 20x 21x 21x 21x 21x 21x 21x 24x 21x 2x 21x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 20x 20x 42x 43x 43x 42x 1x 1x 1x 1x 1x 1x 42x 1x 1x 1x 1x 20x 20x 60x 20x 20x 20x 20x 20x 20x 1x 1x 21x 21x 21x 42x 21x 18x 18x 18x 21x 21x 21x 3x 3x 8x 3x 2x 2x 2x 1x 1x 3x 21x 21x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 19x 38x 8x 1x 10x 1x 14x 2x | <template> <teleport :to="computedTarget" :disabled="renderInPlace"> <transition name="cdx-dialog-fade" appear> <div v-if="open" ref="backdrop" class="cdx-dialog-backdrop" @mousedown="onBackdropMouseDown" @click="onBackdropClick" @keyup.escape="close" > <!-- Focus trap start --> <div ref="focusTrapStart" tabindex="0" @focus="focusLast" /> <div ref="dialogElement" class="cdx-dialog" :class="rootClasses" role="dialog" v-bind="$attrs" :aria-label="$slots.header || hideTitle ? title : undefined" :aria-labelledby="!$slots.header && !hideTitle ? labelId : undefined" aria-modal="true" @click.stop > <header class="cdx-dialog__header" :class="{ 'cdx-dialog__header--default': !$slots.header, 'cdx-dialog__header--no-close-button': !useCloseButtonOrLabel }"I > <!-- @slot Customizable Dialog header --> <slot name="header"> <div v-if="!hideTitle" class="cdx-dialog__header__title-group"> <h2 :id="labelId" class="cdx-dialog__header__title"> {{ title }} </h2> <p v-if="subtitle" class="cdx-dialog__header__subtitle"> {{ subtitle }} </p> </div> <cdx-button class="cdx-dialog__header__close-button" weight="quiet" type="button" :aria-label="translatedCloseButtonLabel" I @click="close" > I <cdx-icon :icon="cdxIconClose" /> I </cdx-button> </slot> </header> <div ref="focusHolder" class="cdx-dialog-focus-trap" tabindex="-1" /> <div ref="dialogBody" class="cdx-dialog__body cdx-scrollable-container" :class="{ 'cdx-dialog__body--no-footer': !( showFooterActions || $slots.footer || $slots[ 'footer-text' ] ) }" > <!-- @slot Dialog content --> <slot /> </div> <footer v-ifE="showFooterActions || $slots.footer || $slots[ 'footer-text' ]" class="cdx-dialog__footer" :class="{ 'cdx-dialog__footer--default': !$slots.footer }" > <!-- @slot Customizable Dialog footer --> <slot name="footer"> <p v-if="$slots[ 'footer-text' ]" class="cdx-dialog__footer__text"> <!-- @slot Optional footer text --> <slot name="footer-text" /> </p> <div v-if="showFooterActions" class="cdx-dialog__footer__actions" > <cdx-button v-if="primaryAction" class="cdx-dialog__footer__primary-action" weight="primary" :action="primaryAction.actionType" :disabled="primaryAction.disabled" @click="$emit( 'primary' )" > {{ primaryAction.label }} </cdx-button> I <cdx-button v-if="defaultAction" class="cdx-dialog__footer__default-action" :disabled="defaultAction.disabled" @click="$emit( 'default' )" > {{ defaultAction.label }} </cdx-button> </div> </slot> </footer> </div> <!-- Focus trap end --> <div ref="focusTrapEnd" tabindex="0" @focus="focusFirst" /> <!-- Teleport target for menus inside this dialog --> <div ref="innerTeleportTarget" /> </div> </transition> </teleport> </template> <script lang="ts"> import { computed, defineComponent, nextTick, toRef, watch, ref, inject, onMounted, onUnmounted, useId, PropType, provide, unref } from 'vue'; import CdxButton from '../button/Button.vue'; import CdxIcon from '../icon/Icon.vue'; import { cdxIconClose } from '@wikimedia/codex-icons'; import useI18nWithOverride from '../../composables/useI18nWithOverride'; import useResizeObserver from '../../composables/useResizeObserver'; import useFocusTrap from '../../composables/useFocusTrap'; import useScrollLock from '../../composables/useScrollLock'; import { TeleportTarget, ModalAction, PrimaryModalAction } from '../../types'; /** * A modal element that overlays the current page, preventing interaction with * other content until it is dismissed. */ export default defineComponent( { name: 'CdxDialog', components: { CdxButton, CdxIcon }, inheritAttrs: false, props: { /** * Whether the dialog is visible. Should be provided via a v-model:open * binding in the parent scope. */ open: { type: Boolean, default: false }, /** * Title for the dialog header. Used for ARIA purposes even if no * visible header element is displayed. */ title: { type: String, required: true }, /** * Optional subtitle for the dialog. */ subtitle: { type: String, required: false, default: null }, /** * Whether the dialog header should hide the title & subtitle */ hideTitle: { type: Boolean, default: false }, /** * Add an icon-only close button to the dialog header. * * On narrow screens, the close button is always displayed. On wide screens, it's only * displayed if this prop is set. */ useCloseButton: { type: Boolean, default: false }, // DEPRECATED: Set default to 'Close' and remove validator (T368444). /** * Visually-hidden label text for the icon-only close button in the header. * * Omit this prop to use the default value, "Close". */ closeButtonLabel: { type: String, default: '', validator: ( value: string, props ) => { if ( value.length > 0 && !props.useCloseButton ) { // eslint-disable-next-line no-console console.warn( `[CdxDialog]: The boolean \`useCloseButton\` prop is required to show the close button.\n Refer to https://doc.wikimedia.org/codex/latest/components/demos/dialog.html#props.` ); return false; } return true; } }, /** * Primary user action. This will display a primary button with the specified action * (progressive or destructive). */ primaryAction: { type: Object as PropType<PrimaryModalAction>, default: null }, /** * Default user action. This will display a normal button. */ defaultAction: { type: Object as PropType<ModalAction>, default: null }, /** * Whether action buttons should be vertically stacked and 100% width. */ stackedActions: { type: Boolean, default: false }, /** * Whether the dialog should maintain a fixed maximum height on mobile screens, * rather than expanding to fit the content height. */ fixedHeight: { type: [ Boolean, Number ], default: false, validator: ( value: unknown ) => typeof value === 'boolean' || ( typeof value === 'number' && value > 0 ) }, /** * Selector or DOM element identifying the container the dialog should * be rendered in. The dialog will be `<teleport>`ed to this element. * An ID selector is recommended, e.g. `#foo-bar`, but providing an * actual element is also supported. * * If this prop is not set, and the parent or one of its ancestors * provides a teleport target using `provide( 'CdxTeleportTarget', * '#foo-bar' )`, the provided target will be used. If there is no * provided target, the dialog will be teleported to the end of the * `<body>` element. */ target: { type: String as PropType<string | HTMLElement | null>, default: null }, /** * Whether to disable the use of teleport and render the Dialog in its * original location in the document. If this is true, the `target` prop * is ignored. */ renderInPlace: { type: Boolean, default: false } }, emits: [ /** * When the open/close state changes, e.g. when the close button is clicked. * * @property {boolean} newValue The new open/close state (true for open, false for closed) */ 'update:open', /** * When the primary action button is clicked. */ 'primary', /** * When the default action button is clicked. */ 'default' ], setup( props, { emit } ) { const labelId = useId(); const backdrop = ref<HTMLDivElement>(); const dialogElement = ref<HTMLDivElement>(); // dialog "frame" const dialogBody = ref<HTMLDivElement>(); // dialog content const innerTeleportTarget = ref<HTMLDivElement>(); // DEPRECATED: require use of new prop useCloseButton (T368444) const useCloseButtonOrLabel = computed( () => props.useCloseButton || props.closeButtonLabel.length > 0 ); const translatedCloseButtonLabel = useI18nWithOverride( toRef( props, 'closeButtonLabel' ), 'cdx-dialog-close-button-label', 'Close' ); const showFooterActions = computed( () => !!props.primaryAction || !!props.defaultAction ); const bodyDimensions = useResizeObserver( dialogBody ); const currentBodyHeight = computed( () => bodyDimensions.value.height ?? 0 ); const showDividers = ref( false ); consEt rootClasses = computed( () => ( { 'cdx-dialog--vertical-actions': props.stackedActions, 'cdx-dialog--dividers': showDividers.value, 'cdx-dialog--fixed-height': props.fixedHeight !== false, 'cdx-dialog--fixed-height-custom': typeof props.fixedHeight === 'number' } ) ); const fixedHeightRem = computed( () => typeof props.fixedHeight === 'number' ? `${ props.fixedHeight / 16 }rem` : '' ); // Determine where to teleport the Dialog to const providedTarget = inject<TeleportTarget>( 'CdxTeleportTarget', undefined ); const computedTarget = computed( () => props.target ?? unref( providedTarget ) ?? 'body' ); // Provide our own inner teleport target as the teleport target to child components provide( 'CdxTeleportTarget', innerTeleportTarget ); const isDialogOpen = computed( () => props.open ); useScrollLock( isDialogOpen ); /** * Close the dialog by emitting an event to the parent */ function close() { emit( 'update:open', false ); } // We close the dialog when the backdrop is clicked, but unfortunately the click event // also fires if the user holds the mouse down inside the dialog, drags the mouse out of // the dialog onto the backdrop, and releases it on the backdrop (T358544). We only want // to close the dialog on a "true" click on the backdrop, so only close the dialog on click // if the preceding mousedown event happened on the backdrop. let mousedownOnBackdrop = false; function onBackdropMouseDown( e: MouseEvent ) { mousedownOnBackdrop = e.target === backdrop.value; } function onBackdropClick() { if ( moEusedownOnBackdrop ) { close(); } } E const { focusTrapStart, focusTrapEnd, focusHolder, focusFirst, focusLast, activateFocusTrap, deactivateFocusTrap, } = useFocusTrap( { // dialogElement is the container for the focus trap containerRef: dialogElement, bodyRef: dialogBody, preventScroll: true } ); let ariaHiddenElements: Element[] = []; let inertElements: Element[] = []; /** * Hide all other elements on the page from screen readers, and prevent user * interaction with them, by setting aria-hidden and inert on all siblings of all * ancestors of the dialog. This leaves the path from the root node to the * dialog as the only nodes that aren't covered by aria-hidden and inert. */ function setAriaHiddenAndInert() { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion let element: HTMLElement = backdrop.value!; while ( element.parentElement && element.nodeName !== 'BODY' ) { for ( const sibling of Array.from( element.parentElement.children ) ) { if ( sibling === element || sibling.nodeName === 'SCRIPT' ) { continue; } // Store the elements we set aria-hidden and inert on, so that we can // unset them when the dialog closes. Exclude elements that already have these // attributes set, so that we don't unset things that were supposed to stay set. if ( !sibling.hasAttribute( 'aria-hidden' ) ) { sibling.setAttribute( 'aria-hidden', 'true' ); ariaHiddenElements.push( sibling ); } if ( !sibling.hasAttribute( 'inert' ) ) { sibling.setAttribute( 'inert', '' ); inertElements.push( sibling ); } } element = element.parentElement; } } function unsetAriaHiddenAndInert() { for ( const element of ariaHiddenElements ) { element.removeAttribute( 'aria-hidden' ); } for ( const element of inertElements ) { element.removeAttribute( 'inert' ); } ariaHiddenElements = []; inertElements = []; } async function onDialogOpen() { // Most of the things below need to happen on nextTick because they rely on template // refs, and those are not yet set when the watcher for props.open runs. await nextTick(); setAriaHiddenAndInert(); // Run shared focus trap setup. await activateFocusTrap(); } function onDialogClose() { unsetAriaHiddenAndInert(); // Run shared focus trap teardown. deactivateFocusTrap(); } // If the dialog is mounted in the open state, make sure we set things up properly onMounted( async () => { if ( props.open ) { await onDialogOpen(); } } ); // If the dialog is closed while it's still mounted, make sure we clean up behind ourselves onUnmounted( () => { if ( props.open ) { onDialogClose(); } } ); watch( toRef( props, 'open' ), async ( opened ) => { if ( opened ) { await onDialogOpen(); } else { onDialogClose(); } } ); // Determine if content dividers should be displayed for overflowing content watch( currentBodyHeight, () => { if ( dialogBody.value ) { showDividers.value = dialogBody.value.clientHeight < dialogBody.value.scrollHeight; } } ); return { close, onBackdropClick, onBackdropMouseDown, cdxIconClose, labelId, rootClasses, backdrop, dialogElement, focusTrapStart, focusTrapEnd, innerTeleportTarget, focusFirst, focusLast, dialogBody, focusHolder, showFooterActions, useCloseButtonOrLabel, translatedCloseButtonLabel, computedTarget, fixedHeightRem }; } } ); </script> <style lang="less"> @import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less'; @import ( reference ) '../../themes/mixins/public/typography.less'; .cdx-dialog-backdrop { background-color: @background-color-backdrop-light; display: flex; align-items: center; justify-content: center; position: fixed; top: 0; left: 0; z-index: @z-index-overlay-backdrop; min-height: @size-full; width: @size-viewport-width-full; height: @size-viewport-height-full; // Support Safari/iOS: Make `100vh` work with Safari's address bar. // See https://stackoverflow.com/questions/37112218/css3-100vh-not-constant-in-mobile-browser /* stylelint-disable-next-line plugin/no-unsupported-browser-features, scale-unlimited/declaration-strict-value */ height: -webkit-fill-available; } .cdx-dialog { background-color: @background-color-base; display: flex; flex-direction: column; box-sizing: @box-sizing-base; // Make dialogs full screen with some space around the sides width: calc( @size-viewport-width-full - ( @size-200) ); height: unset; max-height: calc( @size-viewport-height-full - ( @size-200 ) ); border: @border-base; border-radius: @border-radius-base; box-shadow: @box-shadow-large; &--fixed-height { // Make dialogs full screen with some space around the top and bottom height: calc( @size-viewport-height-full - ( @size-200 ) ); &-custom { /* stylelint-disable-next-line value-keyword-case */ height: v-bind( fixedHeightRem ); } } @media ( min-width: @min-width-breakpoint-tablet ) { // On wide screens, ensure some space around the sides // Dynamically size the height of the dialog to fit the content, up to a limit max-width: @size-3200; } &__header { padding: @spacing-100 @spacing-150 @spacing-50; // If no custom header content is provided, apply these styles to the // `<header>` element &--default { display: flex; align-items: baseline; // Close button should appear at the end regardless of whether or not a title is present justify-content: flex-end; box-sizing: @box-sizing-base; width: @size-full; } &__title-group { display: flex; flex-grow: 1; flex-direction: column; } // Add specificity to override h2 styles, e.g. in MediaWiki skins. // See https://phabricator.wikimedia.org/T324495. & &__title { margin: 0; border: 0; padding: 0; font-family: inherit; font-size: @font-size-x-large; font-weight: @font-weight-bold; line-height: @line-height-x-large; } // Increase specificity to ensure that this style shows up in VitePress. & &__subtitle { color: @color-subtle; margin: 0; padding: 0; font-size: @font-size-medium; line-height: @line-height-small; } &__close-button.cdx-button { margin-right: -@spacing-50; } // Only hide the close button on wide screens; always display it on narrow screens @media ( min-width: @min-width-breakpoint-tablet ) { &--no-close-button &__close-button { display: none; } } /* stylelint-disable-next-line no-descending-specificity */ .cdx-dialog--dividers & { border-bottom: @border-subtle; } } &__body { padding: @spacing-50 @spacing-150; overflow-y: auto; .cdx-mixin-body-text(); // If the dialog does not display a `<footer>` element, add some extra // padding at the bottom of the body &--no-footer { padding-bottom: @spacing-150; } // Zero-out any margin or padding on the first content element that // could disrupt the layout > *:first-child { margin-top: 0; padding-top: 0; } // Zero-out any margin or padding on the last content element that // could disrupt the layout > *:last-child { margin-bottom: 0; padding-bottom: 0; } } &__footer { // Put the footer at the bottom without making the body larger than it needs to be // This ensures the ResizeObserver on the body works even in full-screen mode, which // it would not if we set flex-grow: 1; on the body. margin-top: auto; padding: @spacing-100 @spacing-150 @spacing-150; // If no custom footer content is provided, apply these styles to the // `<footer>` element &--default { display: flex; align-items: baseline; flex-wrap: wrap; justify-content: space-between; gap: @spacing-75; } &__actions { display: flex; flex-grow: 1; flex-direction: row-reverse; gap: @spacing-75; @media ( max-width: @max-width-breakpoint-mobile ) { flex-direction: column; width: @size-full; /* stylelint-disable-next-line max-nesting-depth */ .cdx-button { max-width: none; } } } .cdx-dialog--dividers & { border-top: @border-subtle; } // Increased specificity to ensure that this style shows up in VitePress & &__text { color: @color-subtle; flex: 1 0 auto; width: @spacing-full; margin: 0; font-size: @font-size-small; line-height: @line-height-small; } } &--vertical-actions &__footer__actions { flex-direction: column; width: @size-full; .cdx-button { max-width: none; } } } // This element is not used for visual styling, just // focus management. It needs to be invisible but cannot // have display:none, and its position in the markup is // important (must be inside the dialog element). .cdx-dialog-focus-trap { // Set position to absolute so that this element is // omitted from all flex layout calculations position: absolute; // Don't show visible focus outline for the focus-trap element &:focus { outline: 0; } } .cdx-dialog-fade-enter-active, .cdx-dialog-fade-leave-active { transition-property: @transition-property-fade; // For system initiated transitions – the user clicks a dialog trigger button, // the system opens the dialog – use a longer transition and system timing function. transition-duration: @transition-duration-medium; transition-timing-function: @transition-timing-function-system; } .cdx-dialog-fade-enter-from, .cdx-dialog-fade-leave-to { opacity: @opacity-transparent; } </style> |