@material/web 1.0.0-pre.1 → 1.0.0-pre.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -19
- package/autocomplete/lib/_filled-autocomplete.scss +1 -34
- package/autocomplete/lib/_outlined-autocomplete.scss +1 -34
- package/autocomplete/lib/autocomplete.d.ts +0 -4
- package/autocomplete/lib/autocomplete.js +5 -12
- package/autocomplete/lib/autocomplete.js.map +1 -1
- package/autocomplete/lib/autocompleteitem/autocomplete-item.d.ts +2 -2
- package/autocomplete/lib/autocompleteitem/autocomplete-item.js +2 -2
- package/autocomplete/lib/autocompleteitem/autocomplete-item.js.map +1 -1
- package/autocomplete/lib/autocompletelist/autocomplete-list.d.ts +1 -1
- package/autocomplete/lib/autocompletelist/autocomplete-list.js +2 -2
- package/autocomplete/lib/autocompletelist/autocomplete-list.js.map +1 -1
- package/autocomplete/lib/filled-styles.css.js +1 -1
- package/autocomplete/lib/filled-styles.css.js.map +1 -1
- package/autocomplete/lib/outlined-styles.css.js +1 -1
- package/autocomplete/lib/outlined-styles.css.js.map +1 -1
- package/button/lib/_elevation.scss +1 -1
- package/button/lib/_icon.scss +14 -24
- package/button/lib/_shared.scss +1 -0
- package/button/lib/button.d.ts +0 -3
- package/button/lib/button.js +1 -12
- package/button/lib/button.js.map +1 -1
- package/button/lib/filled-styles.css.js +1 -1
- package/button/lib/filled-styles.css.js.map +1 -1
- package/button/lib/link-button.d.ts +0 -1
- package/button/lib/link-button.js +0 -5
- package/button/lib/link-button.js.map +1 -1
- package/button/lib/shared-elevation-styles.css.js +1 -1
- package/button/lib/shared-elevation-styles.css.js.map +1 -1
- package/button/lib/shared-styles.css.js +1 -1
- package/button/lib/shared-styles.css.js.map +1 -1
- package/checkbox/checkbox.d.ts +11 -2
- package/checkbox/checkbox.js +11 -2
- package/checkbox/checkbox.js.map +1 -1
- package/checkbox/lib/checkbox.d.ts +28 -0
- package/checkbox/lib/checkbox.js +30 -1
- package/checkbox/lib/checkbox.js.map +1 -1
- package/chips/chip/lib/_chip-theme.scss +1 -1
- package/dialog/_dialog.scss +6 -0
- package/dialog/dialog.d.ts +38 -0
- package/dialog/dialog.js +41 -0
- package/dialog/dialog.js.map +1 -0
- package/dialog/harness.d.ts +18 -0
- package/dialog/harness.js +55 -0
- package/dialog/harness.js.map +1 -0
- package/dialog/lib/_dialog.scss +386 -0
- package/dialog/lib/_tokens.scss +86 -0
- package/{list/lib/divider/list-divider-styles.css.d.ts → dialog/lib/dialog-styles.css.d.ts} +0 -0
- package/dialog/lib/dialog-styles.css.js +9 -0
- package/dialog/lib/dialog-styles.css.js.map +1 -0
- package/dialog/lib/dialog-styles.scss +8 -0
- package/dialog/lib/dialog.d.ts +190 -0
- package/dialog/lib/dialog.js +566 -0
- package/dialog/lib/dialog.js.map +1 -0
- package/divider/_divider.scss +6 -0
- package/divider/divider.d.ts +24 -0
- package/divider/divider.js +27 -0
- package/divider/divider.js.map +1 -0
- package/divider/lib/_divider.scss +54 -0
- package/{menu/lib/menu-button-styles.css.d.ts → divider/lib/divider-styles.css.d.ts} +0 -0
- package/divider/lib/divider-styles.css.js +9 -0
- package/divider/lib/divider-styles.css.js.map +1 -0
- package/divider/lib/divider-styles.scss +8 -0
- package/divider/lib/divider.d.ts +23 -0
- package/divider/lib/divider.js +41 -0
- package/divider/lib/divider.js.map +1 -0
- package/elevation/lib/_elevation.scss +1 -1
- package/elevation/lib/_md-comp-elevation.scss +1 -1
- package/elevation/lib/elevation-styles.css.js +1 -1
- package/elevation/lib/elevation-styles.css.js.map +1 -1
- package/fab/lib/_shared.scss +1 -1
- package/fab/lib/fab-shared-styles.css.js +1 -1
- package/fab/lib/fab-shared-styles.css.js.map +1 -1
- package/field/lib/_filled-field.scss +5 -2
- package/field/lib/_md-comp-filled-field.scss +3 -1
- package/field/lib/_md-comp-outlined-field.scss +1 -1
- package/field/lib/_shared.scss +2 -0
- package/field/lib/field.d.ts +0 -2
- package/field/lib/field.js +2 -4
- package/field/lib/field.js.map +1 -1
- package/field/lib/filled-styles.css.js +1 -1
- package/field/lib/filled-styles.css.js.map +1 -1
- package/field/lib/outlined-styles.css.js +1 -1
- package/field/lib/outlined-styles.css.js.map +1 -1
- package/field/lib/shared-styles.css.js +1 -1
- package/field/lib/shared-styles.css.js.map +1 -1
- package/focus/lib/_focus-ring.scss +3 -10
- package/focus/lib/focus-ring-styles.css.js +1 -1
- package/focus/lib/focus-ring-styles.css.js.map +1 -1
- package/focus/lib/focus-ring.d.ts +1 -4
- package/focus/lib/focus-ring.js +2 -11
- package/focus/lib/focus-ring.js.map +1 -1
- package/iconbutton/lib/_filled-icon-button.scss +1 -1
- package/iconbutton/lib/_filled-tonal-icon-button.scss +1 -1
- package/iconbutton/lib/_outlined-icon-button.scss +7 -5
- package/iconbutton/lib/_shared.scss +5 -25
- package/iconbutton/lib/_standard-icon-button.scss +4 -6
- package/iconbutton/lib/filled-styles.css.js +1 -1
- package/iconbutton/lib/filled-styles.css.js.map +1 -1
- package/iconbutton/lib/filled-tonal-styles.css.js +1 -1
- package/iconbutton/lib/filled-tonal-styles.css.js.map +1 -1
- package/iconbutton/lib/icon-button-toggle.d.ts +13 -42
- package/iconbutton/lib/icon-button-toggle.js +33 -103
- package/iconbutton/lib/icon-button-toggle.js.map +1 -1
- package/iconbutton/lib/icon-button.d.ts +2 -4
- package/iconbutton/lib/icon-button.js +3 -9
- package/iconbutton/lib/icon-button.js.map +1 -1
- package/iconbutton/lib/outlined-styles.css.js +1 -1
- package/iconbutton/lib/outlined-styles.css.js.map +1 -1
- package/iconbutton/lib/shared-styles.css.js +1 -1
- package/iconbutton/lib/shared-styles.css.js.map +1 -1
- package/iconbutton/lib/standard-styles.css.js +1 -1
- package/iconbutton/lib/standard-styles.css.js.map +1 -1
- package/list/lib/_list.scss +32 -40
- package/list/lib/avatar/_list-item-avatar.scss +1 -1
- package/list/lib/avatar/list-item-avatar-styles.css.js +1 -1
- package/list/lib/avatar/list-item-avatar-styles.css.js.map +1 -1
- package/list/lib/avatar/list-item-avatar.d.ts +9 -4
- package/list/lib/avatar/list-item-avatar.js +24 -11
- package/list/lib/avatar/list-item-avatar.js.map +1 -1
- package/list/lib/icon/_list-item-icon.scss +3 -1
- package/list/lib/icon/list-item-icon-styles.css.js +1 -1
- package/list/lib/icon/list-item-icon-styles.css.js.map +1 -1
- package/list/lib/icon/list-item-icon.d.ts +0 -3
- package/list/lib/icon/list-item-icon.js +1 -12
- package/list/lib/icon/list-item-icon.js.map +1 -1
- package/list/lib/image/_list-item-image.scss +1 -1
- package/list/lib/image/list-item-image-styles.css.js +1 -1
- package/list/lib/image/list-item-image-styles.css.js.map +1 -1
- package/list/lib/image/list-item-image.d.ts +11 -4
- package/list/lib/image/list-item-image.js +24 -13
- package/list/lib/image/list-item-image.js.map +1 -1
- package/list/lib/list-styles.css.js +1 -1
- package/list/lib/list-styles.css.js.map +1 -1
- package/list/lib/list.d.ts +99 -28
- package/list/lib/list.js +210 -111
- package/list/lib/list.js.map +1 -1
- package/list/lib/listitem/_list-item.scss +104 -39
- package/list/lib/listitem/harness.js +2 -1
- package/list/lib/listitem/harness.js.map +1 -1
- package/{tokens/v0_150/index.test.css.d.ts → list/lib/listitem/list-item-private-styles.css.d.ts} +0 -0
- package/list/lib/listitem/list-item-private-styles.css.js +9 -0
- package/list/lib/listitem/list-item-private-styles.css.js.map +1 -0
- package/list/lib/listitem/list-item-private-styles.scss +8 -0
- package/list/lib/listitem/list-item-styles.css.js +1 -1
- package/list/lib/listitem/list-item-styles.css.js.map +1 -1
- package/list/lib/listitem/list-item.d.ts +99 -43
- package/list/lib/listitem/list-item.js +201 -172
- package/list/lib/listitem/list-item.js.map +1 -1
- package/list/lib/listitemlink/list-item-link.d.ts +17 -0
- package/list/lib/listitemlink/list-item-link.js +42 -0
- package/list/lib/listitemlink/list-item-link.js.map +1 -0
- package/list/lib/video/_list-item-video.scss +10 -4
- package/list/lib/video/list-item-video-styles.css.js +1 -1
- package/list/lib/video/list-item-video-styles.css.js.map +1 -1
- package/list/lib/video/list-item-video.d.ts +43 -4
- package/list/lib/video/list-item-video.js +90 -12
- package/list/lib/video/list-item-video.js.map +1 -1
- package/list/list-item-avatar.d.ts +2 -1
- package/list/list-item-avatar.js +2 -1
- package/list/list-item-avatar.js.map +1 -1
- package/list/list-item-icon.d.ts +2 -1
- package/list/list-item-icon.js +2 -1
- package/list/list-item-icon.js.map +1 -1
- package/list/list-item-image.d.ts +2 -1
- package/list/list-item-image.js +2 -1
- package/list/list-item-image.js.map +1 -1
- package/list/list-item-link.d.ts +35 -0
- package/list/list-item-link.js +39 -0
- package/list/list-item-link.js.map +1 -0
- package/list/list-item-video.d.ts +2 -1
- package/list/list-item-video.js +2 -1
- package/list/list-item-video.js.map +1 -1
- package/list/list-item.d.ts +18 -2
- package/list/list-item.js +20 -3
- package/list/list-item.js.map +1 -1
- package/list/list.d.ts +15 -1
- package/list/list.js +15 -1
- package/list/list.js.map +1 -1
- package/menu/_menu-item.scss +6 -0
- package/menu/_menu.scss +6 -0
- package/menu/harness.d.ts +5 -0
- package/menu/harness.js +22 -0
- package/menu/harness.js.map +1 -1
- package/menu/lib/_menu.scss +61 -62
- package/menu/lib/menu-styles.css.js +1 -1
- package/menu/lib/menu-styles.css.js.map +1 -1
- package/menu/lib/menu.d.ts +176 -42
- package/menu/lib/menu.js +506 -246
- package/menu/lib/menu.js.map +1 -1
- package/menu/lib/menuitem/_menu-item.scss +115 -0
- package/{tokens/v0_150/lib.test.css.d.ts → menu/lib/menuitem/menu-item-private-styles.css.d.ts} +0 -0
- package/menu/lib/menuitem/menu-item-private-styles.css.js +9 -0
- package/menu/lib/menuitem/menu-item-private-styles.css.js.map +1 -0
- package/menu/lib/menuitem/menu-item-private-styles.scss +8 -0
- package/menu/lib/menuitem/menu-item-styles.css.d.ts +1 -0
- package/menu/lib/menuitem/menu-item-styles.css.js +9 -0
- package/menu/lib/menuitem/menu-item-styles.css.js.map +1 -0
- package/menu/lib/menuitem/menu-item-styles.scss +8 -0
- package/menu/lib/menuitem/menu-item.d.ts +20 -3
- package/menu/lib/menuitem/menu-item.js +42 -3
- package/menu/lib/menuitem/menu-item.js.map +1 -1
- package/menu/lib/menuitemlink/menu-item-link.d.ts +25 -0
- package/menu/lib/menuitemlink/menu-item-link.js +51 -0
- package/menu/lib/menuitemlink/menu-item-link.js.map +1 -0
- package/menu/lib/shared.d.ts +134 -0
- package/menu/lib/shared.js +85 -0
- package/menu/lib/shared.js.map +1 -0
- package/menu/lib/submenuitem/harness.d.ts +11 -0
- package/menu/lib/submenuitem/harness.js +12 -0
- package/menu/lib/submenuitem/harness.js.map +1 -0
- package/menu/lib/submenuitem/sub-menu-item.d.ts +89 -0
- package/menu/lib/submenuitem/sub-menu-item.js +266 -0
- package/menu/lib/submenuitem/sub-menu-item.js.map +1 -0
- package/menu/lib/surfacePositionController.d.ts +117 -0
- package/menu/lib/surfacePositionController.js +196 -0
- package/menu/lib/surfacePositionController.js.map +1 -0
- package/menu/lib/typeaheadController.d.ts +144 -0
- package/menu/lib/typeaheadController.js +242 -0
- package/menu/lib/typeaheadController.js.map +1 -0
- package/menu/menu-item-link.d.ts +33 -0
- package/menu/menu-item-link.js +37 -0
- package/menu/menu-item-link.js.map +1 -0
- package/menu/menu-item.d.ts +19 -2
- package/menu/menu-item.js +22 -4
- package/menu/menu-item.js.map +1 -1
- package/menu/menu.d.ts +45 -0
- package/menu/menu.js +43 -0
- package/menu/menu.js.map +1 -1
- package/menu/sub-menu-item.d.ts +60 -0
- package/menu/sub-menu-item.js +64 -0
- package/menu/sub-menu-item.js.map +1 -0
- package/menusurface/lib/_menu-surface.scss +1 -1
- package/menusurface/lib/menu-surface-styles.css.js +1 -1
- package/menusurface/lib/menu-surface-styles.css.js.map +1 -1
- package/motion/animation.d.ts +20 -3
- package/motion/animation.js +39 -4
- package/motion/animation.js.map +1 -1
- package/navigationbar/lib/_navigation-bar.scss +9 -9
- package/navigationbar/lib/navigation-bar-styles.css.js +1 -1
- package/navigationbar/lib/navigation-bar-styles.css.js.map +1 -1
- package/navigationdrawer/lib/_navigation-drawer.scss +1 -1
- package/navigationdrawer/lib/navigation-drawer-styles.css.js +1 -1
- package/navigationdrawer/lib/navigation-drawer-styles.css.js.map +1 -1
- package/navigationtab/lib/_navigation-tab.scss +8 -19
- package/navigationtab/lib/navigation-tab-styles.css.js +1 -1
- package/navigationtab/lib/navigation-tab-styles.css.js.map +1 -1
- package/package.json +1 -8
- package/radio/lib/_radio.scss +0 -1
- package/radio/lib/radio-styles.css.js +1 -1
- package/radio/lib/radio-styles.css.js.map +1 -1
- package/radio/lib/radio.d.ts +10 -1
- package/radio/lib/radio.js +10 -1
- package/radio/lib/radio.js.map +1 -1
- package/radio/radio.d.ts +16 -1
- package/radio/radio.js +16 -1
- package/radio/radio.js.map +1 -1
- package/ripple/lib/_ripple.scss +5 -21
- package/ripple/lib/ripple-styles.css.js +1 -1
- package/ripple/lib/ripple-styles.css.js.map +1 -1
- package/ripple/lib/ripple.js +2 -2
- package/ripple/lib/ripple.js.map +1 -1
- package/sass/_map-ext.scss +24 -0
- package/sass/_string-ext.scss +23 -0
- package/segmentedbuttonset/lib/segmented-button-set.d.ts +0 -2
- package/segmentedbuttonset/lib/segmented-button-set.js +0 -2
- package/segmentedbuttonset/lib/segmented-button-set.js.map +1 -1
- package/switch/lib/_handle.scss +56 -9
- package/switch/lib/_icon.scss +36 -0
- package/switch/lib/_switch.scss +1 -115
- package/switch/lib/_track.scss +39 -0
- package/switch/lib/switch-styles.css.js +1 -1
- package/switch/lib/switch-styles.css.js.map +1 -1
- package/switch/lib/switch.d.ts +3 -0
- package/switch/lib/switch.js +3 -0
- package/switch/lib/switch.js.map +1 -1
- package/textfield/lib/_filled-text-field.scss +4 -2
- package/textfield/lib/_outlined-text-field.scss +3 -2
- package/textfield/lib/filled-styles.css.js +1 -1
- package/textfield/lib/filled-styles.css.js.map +1 -1
- package/textfield/lib/outlined-styles.css.js +1 -1
- package/textfield/lib/outlined-styles.css.js.map +1 -1
- package/textfield/lib/text-field.d.ts +0 -2
- package/textfield/lib/text-field.js +0 -2
- package/textfield/lib/text-field.js.map +1 -1
- package/tokens/_index.scss +1 -1
- package/tokens/{v0_150 → v0_152}/_index.scss +1 -1
- package/tokens/{v0_150 → v0_152}/_md-comp-assist-chip.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-badge.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-banner.scss +2 -43
- package/tokens/{v0_150 → v0_152}/_md-comp-bottom-app-bar.scss +3 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-carousel-item.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-checkbox.scss +2 -12
- package/tokens/{v0_150 → v0_152}/_md-comp-circular-progress-indicator.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-data-table.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-date-input-modal.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-date-picker-docked.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-date-picker-modal.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-dialog.scss +2 -25
- package/tokens/{v0_150 → v0_152}/_md-comp-divider.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-elevated-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-elevated-card.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-extended-fab-branded.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-extended-fab-primary.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-extended-fab-secondary.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-extended-fab-surface.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-extended-fab-tertiary.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-branded-large.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-branded.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-primary-large.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-primary-small.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-primary.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-secondary-large.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-secondary-small.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-secondary.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-surface-large.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-surface-small.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-surface.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-tertiary-large.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-tertiary-small.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-fab-tertiary.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-autocomplete.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-button.scss +2 -8
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-card.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-icon-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-menu-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-select.scss +2 -6
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-text-field.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-tonal-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-filled-tonal-icon-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-filter-chip.scss +2 -30
- package/tokens/{v0_150 → v0_152}/_md-comp-full-screen-dialog.scss +3 -53
- package/tokens/{v0_150 → v0_152}/_md-comp-icon-button.scss +3 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-input-chip.scss +2 -52
- package/tokens/{v0_150 → v0_152}/_md-comp-linear-progress-indicator.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-list.scss +3 -12
- package/tokens/{v0_150 → v0_152}/_md-comp-menu.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-navigation-bar.scss +2 -9
- package/tokens/{v0_150 → v0_152}/_md-comp-navigation-drawer.scss +2 -8
- package/tokens/{v0_150 → v0_152}/_md-comp-navigation-rail.scss +2 -39
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-autocomplete.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-button.scss +2 -6
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-card.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-icon-button.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-menu-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-segmented-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-select.scss +2 -6
- package/tokens/{v0_150 → v0_152}/_md-comp-outlined-text-field.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-plain-tooltip.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-primary-navigation-tab.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-radio-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-rich-tooltip.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-scrim.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-search-bar.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-search-view.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-secondary-navigation-tab.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-sheet-bottom.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-sheet-floating.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-sheet-side.scss +4 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-slider.scss +2 -4
- package/tokens/{v0_150 → v0_152}/_md-comp-snackbar.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-standard-menu-button.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-suggestion-chip.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-switch.scss +2 -11
- package/tokens/{v0_150 → v0_152}/_md-comp-text-button.scss +2 -6
- package/tokens/{v0_150 → v0_152}/_md-comp-time-input.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-time-picker.scss +2 -5
- package/tokens/{v0_150 → v0_152}/_md-comp-top-app-bar-large.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-top-app-bar-medium.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-comp-top-app-bar-small-centered.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-comp-top-app-bar-small.scss +2 -3
- package/tokens/{v0_150 → v0_152}/_md-ref-palette.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-ref-typeface.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-sys-color.scss +2 -20
- package/tokens/{v0_150 → v0_152}/_md-sys-elevation.scss +4 -13
- package/tokens/{v0_150 → v0_152}/_md-sys-motion.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-sys-shape.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-sys-state.scss +2 -2
- package/tokens/{v0_150 → v0_152}/_md-sys-typescale.scss +2 -3
- package/tokens/v0_152/index.test.css.d.ts +1 -0
- package/tokens/{v0_150 → v0_152}/index.test.css.js +0 -0
- package/tokens/{v0_150 → v0_152}/index.test.css.js.map +0 -0
- package/tokens/{v0_150 → v0_152}/index.test.scss +1 -1
- package/tokens/v0_152/lib.test.css.d.ts +1 -0
- package/tokens/{v0_150 → v0_152}/lib.test.css.js +0 -0
- package/tokens/{v0_150 → v0_152}/lib.test.css.js.map +0 -0
- package/tokens/{v0_150 → v0_152}/lib.test.scss +1 -1
- package/list/lib/_tokens.scss +0 -80
- package/list/lib/divider/_list-divider.scss +0 -46
- package/list/lib/divider/list-divider-styles.css.js +0 -9
- package/list/lib/divider/list-divider-styles.css.js.map +0 -1
- package/list/lib/divider/list-divider-styles.scss +0 -8
- package/list/lib/divider/list-divider.d.ts +0 -13
- package/list/lib/divider/list-divider.js +0 -32
- package/list/lib/divider/list-divider.js.map +0 -1
- package/list/list-divider.d.ts +0 -19
- package/list/list-divider.js +0 -22
- package/list/list-divider.js.map +0 -1
- package/menu/lib/_menu-button.scss +0 -14
- package/menu/lib/adapter.d.ts +0 -66
- package/menu/lib/adapter.js +0 -7
- package/menu/lib/adapter.js.map +0 -1
- package/menu/lib/constants.d.ts +0 -22
- package/menu/lib/constants.js +0 -23
- package/menu/lib/constants.js.map +0 -1
- package/menu/lib/foundation.d.ts +0 -49
- package/menu/lib/foundation.js +0 -123
- package/menu/lib/foundation.js.map +0 -1
- package/menu/lib/menu-button-styles.css.js +0 -9
- package/menu/lib/menu-button-styles.css.js.map +0 -1
- package/menu/lib/menu-button-styles.scss +0 -8
- package/menu/lib/menu-button.d.ts +0 -27
- package/menu/lib/menu-button.js +0 -93
- package/menu/lib/menu-button.js.map +0 -1
- package/menu/menu-button.d.ts +0 -14
- package/menu/menu-button.js +0 -17
- package/menu/menu-button.js.map +0 -1
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Given a surface, an anchor, corners, and some options, this surface will
|
|
8
|
+
* calculate the position of a surface to align the two given corners and keep
|
|
9
|
+
* the surface inside the window viewport. It also provides a StyleInfo map that
|
|
10
|
+
* can be applied to the surface to handle visiblility and position.
|
|
11
|
+
*/
|
|
12
|
+
export class SurfacePositionController {
|
|
13
|
+
/**
|
|
14
|
+
* @param host The host to connect the controller to.
|
|
15
|
+
* @param getProperties A function that returns the properties for the
|
|
16
|
+
* controller.
|
|
17
|
+
*/
|
|
18
|
+
constructor(host, getProperties) {
|
|
19
|
+
this.host = host;
|
|
20
|
+
this.getProperties = getProperties;
|
|
21
|
+
// The current styles to apply to the surface.
|
|
22
|
+
this.surfaceStylesInternal = {
|
|
23
|
+
'display': 'none',
|
|
24
|
+
};
|
|
25
|
+
// Previous values stored for change detection. Open change detection is
|
|
26
|
+
// calculated separately so initialize it here.
|
|
27
|
+
this.lastValues = { isOpen: false };
|
|
28
|
+
this.host.addController(this);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The StyleInfo map to apply to the surface via Lit's stylemap
|
|
32
|
+
*/
|
|
33
|
+
get surfaceStyles() {
|
|
34
|
+
return this.surfaceStylesInternal;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Calculates the surface's new position required so that the surface's
|
|
38
|
+
* `surfaceCorner` aligns to the anchor's `anchorCorner` while keeping the
|
|
39
|
+
* surface inside the window viewport. This positioning also respects RTL by
|
|
40
|
+
* checking `getComputedStyle()` on the surface element.
|
|
41
|
+
*/
|
|
42
|
+
async position() {
|
|
43
|
+
const { surfaceEl, anchorEl, anchorCorner: anchorCornerRaw, surfaceCorner: surfaceCornerRaw, isTopLayer: topLayerRaw, xOffset, yOffset, } = this.getProperties();
|
|
44
|
+
const anchorCorner = anchorCornerRaw.toUpperCase().trim();
|
|
45
|
+
const surfaceCorner = surfaceCornerRaw.toUpperCase().trim();
|
|
46
|
+
if (!surfaceEl || !anchorEl) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
// Paint the surface transparently so that we can get the position and the
|
|
50
|
+
// rect info of the surface.
|
|
51
|
+
this.surfaceStylesInternal = {
|
|
52
|
+
'display': 'block',
|
|
53
|
+
'opacity': '0',
|
|
54
|
+
};
|
|
55
|
+
// Wait for it to be visible.
|
|
56
|
+
this.host.requestUpdate();
|
|
57
|
+
await this.host.updateComplete;
|
|
58
|
+
const surfaceRect = surfaceEl.getBoundingClientRect();
|
|
59
|
+
const anchorRect = anchorEl.getBoundingClientRect();
|
|
60
|
+
const [surfaceBlock, surfaceInline] = surfaceCorner.split('_');
|
|
61
|
+
const [anchorBlock, anchorInline] = anchorCorner.split('_');
|
|
62
|
+
// We use number booleans to multiply values rather than `if` / ternary
|
|
63
|
+
// statements because it _heavily_ cuts down on nesting and readability
|
|
64
|
+
const isTopLayer = topLayerRaw ? 1 : 0;
|
|
65
|
+
// LTR depends on the direction of the SURFACE not the anchor.
|
|
66
|
+
const isLTR = getComputedStyle(surfaceEl).direction === 'ltr' ? 1 : 0;
|
|
67
|
+
const isRTL = isLTR ? 0 : 1;
|
|
68
|
+
const isSurfaceInlineStart = surfaceInline === 'START' ? 1 : 0;
|
|
69
|
+
const isSurfaceInlineEnd = surfaceInline === 'END' ? 1 : 0;
|
|
70
|
+
const isSurfaceBlockStart = surfaceBlock === 'START' ? 1 : 0;
|
|
71
|
+
const isSurfaceBlockEnd = surfaceBlock === 'END' ? 1 : 0;
|
|
72
|
+
const isOneInlineEnd = anchorInline !== surfaceInline ? 1 : 0;
|
|
73
|
+
const isOneBlockEnd = anchorBlock !== surfaceBlock ? 1 : 0;
|
|
74
|
+
/*
|
|
75
|
+
* A diagram that helps describe some of the variables used in the following
|
|
76
|
+
* calculations.
|
|
77
|
+
*
|
|
78
|
+
* ┌───── inline/blockTopLayerOffset
|
|
79
|
+
* │ │
|
|
80
|
+
* │ ┌─▼───┐ Window
|
|
81
|
+
* │ ┌┼─────┴────────────────────────┐
|
|
82
|
+
* │ ││ │
|
|
83
|
+
* └──► ││ ┌──inline/blockAnchorOffset │
|
|
84
|
+
* ││ │ │ │
|
|
85
|
+
* └┤ │ ┌──▼───┐ │
|
|
86
|
+
* │ │ ┌┼──────┤ │
|
|
87
|
+
* │ └─►│Anchor│ │
|
|
88
|
+
* │ └┴──────┘ │
|
|
89
|
+
* │ │
|
|
90
|
+
* │ ┌────────────────────────┼────┐
|
|
91
|
+
* │ │ Surface │ │
|
|
92
|
+
* │ │ │ │
|
|
93
|
+
* │ │ │ │
|
|
94
|
+
* │ │ │ │
|
|
95
|
+
* │ │ │ │
|
|
96
|
+
* │ │ │ │
|
|
97
|
+
* └─────┼────────────────────────┘ ├┐
|
|
98
|
+
* │ inline/blockOOBCorrection ││
|
|
99
|
+
* │ │ ││
|
|
100
|
+
* │ ├──►││
|
|
101
|
+
* │ │ ││
|
|
102
|
+
* └────────────────────────┐▼───┼┘
|
|
103
|
+
* └────┘
|
|
104
|
+
*/
|
|
105
|
+
// Whether or not to apply the width of the anchor
|
|
106
|
+
const inlineAnchorOffset = isOneInlineEnd * anchorRect.width + xOffset;
|
|
107
|
+
// The inline position of the anchor relative to window in LTR
|
|
108
|
+
const inlineTopLayerOffsetLTR = isSurfaceInlineStart * anchorRect.left +
|
|
109
|
+
isSurfaceInlineEnd * (window.innerWidth - anchorRect.right);
|
|
110
|
+
// The inline position of the anchor relative to window in RTL
|
|
111
|
+
const inlineTopLayerOffsetRTL = isSurfaceInlineStart * (window.innerWidth - anchorRect.right) +
|
|
112
|
+
isSurfaceInlineEnd * anchorRect.left;
|
|
113
|
+
// The inline position of the anchor relative to window
|
|
114
|
+
const inlineTopLayerOffset = isLTR * inlineTopLayerOffsetLTR + isRTL * inlineTopLayerOffsetRTL;
|
|
115
|
+
// If the surface's inline would be out of bounds of the window, move it
|
|
116
|
+
// back in
|
|
117
|
+
const inlineOutOfBoundsCorrection = Math.min(0, window.innerWidth - inlineTopLayerOffset - inlineAnchorOffset -
|
|
118
|
+
surfaceRect.width);
|
|
119
|
+
// The inline logical value of the surface
|
|
120
|
+
const inline = isTopLayer * inlineTopLayerOffset + inlineAnchorOffset +
|
|
121
|
+
inlineOutOfBoundsCorrection;
|
|
122
|
+
// Whether or not to apply the height of the anchor
|
|
123
|
+
const blockAnchorOffset = isOneBlockEnd * anchorRect.height + yOffset;
|
|
124
|
+
// The absolute block position of the anchor relative to window
|
|
125
|
+
const blockTopLayerOffset = isSurfaceBlockStart * anchorRect.top +
|
|
126
|
+
isSurfaceBlockEnd * (window.innerHeight - anchorRect.bottom);
|
|
127
|
+
// If the surface's block would be out of bounds of the window, move it back
|
|
128
|
+
// in
|
|
129
|
+
const blockOutOfBoundsCorrection = Math.min(0, window.innerHeight - blockTopLayerOffset - blockAnchorOffset -
|
|
130
|
+
surfaceRect.height);
|
|
131
|
+
// The block logical value of the surface
|
|
132
|
+
const block = isTopLayer * blockTopLayerOffset + blockAnchorOffset +
|
|
133
|
+
blockOutOfBoundsCorrection;
|
|
134
|
+
const surfaceBlockProperty = surfaceBlock === 'START' ? 'inset-block-start' : 'inset-block-end';
|
|
135
|
+
const surfaceInlineProperty = surfaceInline === 'START' ? 'inset-inline-start' : 'inset-inline-end';
|
|
136
|
+
this.surfaceStylesInternal = {
|
|
137
|
+
'display': 'block',
|
|
138
|
+
'opacity': '1',
|
|
139
|
+
[surfaceBlockProperty]: `${block}px`,
|
|
140
|
+
[surfaceInlineProperty]: `${inline}px`,
|
|
141
|
+
};
|
|
142
|
+
this.host.requestUpdate();
|
|
143
|
+
}
|
|
144
|
+
hostUpdate() {
|
|
145
|
+
this.onUpdate();
|
|
146
|
+
}
|
|
147
|
+
hostUpdated() {
|
|
148
|
+
this.onUpdate();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Checks whether the properties passed into the controller have changed since
|
|
152
|
+
* the last positioning. If so, it will reposition if the surface is open or
|
|
153
|
+
* close it if the surface should close.
|
|
154
|
+
*/
|
|
155
|
+
async onUpdate() {
|
|
156
|
+
const props = this.getProperties();
|
|
157
|
+
let hasChanged = false;
|
|
158
|
+
for (const [key, value] of Object.entries(props)) {
|
|
159
|
+
// tslint:disable-next-line
|
|
160
|
+
hasChanged = hasChanged || (value !== this.lastValues[key]);
|
|
161
|
+
if (hasChanged)
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
const openChanged = this.lastValues.isOpen !== props.isOpen;
|
|
165
|
+
const hasAnchor = !!props.anchorEl;
|
|
166
|
+
const hasSurface = !!props.surfaceEl;
|
|
167
|
+
if (hasChanged && hasAnchor && hasSurface) {
|
|
168
|
+
// Only update isOpen, because if it's closed, we do not want to waste
|
|
169
|
+
// time on a useless reposition calculation. So save the other "dirty"
|
|
170
|
+
// values until next time it opens.
|
|
171
|
+
this.lastValues.isOpen = props.isOpen;
|
|
172
|
+
if (props.isOpen) {
|
|
173
|
+
// We are going to do a reposition, so save the prop values for future
|
|
174
|
+
// dirty checking.
|
|
175
|
+
this.lastValues = props;
|
|
176
|
+
await this.position();
|
|
177
|
+
props.onOpen();
|
|
178
|
+
}
|
|
179
|
+
else if (openChanged) {
|
|
180
|
+
await props.beforeClose();
|
|
181
|
+
this.close();
|
|
182
|
+
props.onClose();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Hides the surface.
|
|
188
|
+
*/
|
|
189
|
+
close() {
|
|
190
|
+
this.surfaceStylesInternal = {
|
|
191
|
+
'display': 'none',
|
|
192
|
+
};
|
|
193
|
+
this.host.requestUpdate();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=surfacePositionController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"surfacePositionController.js","sourceRoot":"","sources":["surfacePositionController.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA4EH;;;;;GAKG;AACH,MAAM,OAAO,yBAAyB;IAUpC;;;;OAIG;IACH,YACqB,IAA4B,EAC5B,aAAwD;QADxD,SAAI,GAAJ,IAAI,CAAwB;QAC5B,kBAAa,GAAb,aAAa,CAA2C;QAhB7E,8CAA8C;QACtC,0BAAqB,GAAc;YACzC,SAAS,EAAE,MAAM;SAClB,CAAC;QACF,wEAAwE;QACxE,+CAA+C;QACvC,eAAU,GAAwC,EAAC,MAAM,EAAE,KAAK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AAW,EAAE;gBACtB,MAAM,KAAK,CAAC,WAAW,EAAE,CAAC;gBAC1B,IAAI,CAAC,KAAK,EAAE,CAAC;gBACb,KAAK,CAAC,OAAO,EAAE,CAAC;aACjB;SACF;IACH,CAAC;IAED;;OAEG;IACK,KAAK;QACX,IAAI,CAAC,qBAAqB,GAAG;YAC3B,SAAS,EAAE,MAAM;SAClB,CAAC;QACF,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IAC5B,CAAC;CACF","sourcesContent":["/**\n * @license\n * Copyright 2023 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {ReactiveController, ReactiveControllerHost} from 'lit';\nimport {StyleInfo} from 'lit/directives/style-map.js';\n\n/**\n * A corner of a box in the standard logical property style of <block>_<inline>\n */\nexport type Corner = 'END_START'|'END_END'|'START_START'|'START_END';\n\n/**\n * The configurable options for the surface position controller.\n */\nexport interface SurfacePositionControllerProperties {\n /**\n * The corner of the anchor to align the surface's position.\n */\n anchorCorner: Corner;\n /**\n * The corner of the surface to align to the given anchor corner.\n */\n surfaceCorner: Corner;\n /**\n * The HTMLElement reference of the surface to be positioned.\n */\n surfaceEl: HTMLElement|null;\n /**\n * The HTMLElement reference of the anchor to align to.\n */\n anchorEl: HTMLElement|null;\n /**\n * Whether or not the calculation should be relative to the top layer rather\n * than relative to the parent of the anchor.\n *\n * Examples for `isTopLayer:true`:\n *\n * - If there is no `position:relative` in the given parent tree and the\n * surface is `position:absolute`\n * - If the surface is `position:fixed`\n * - If the surface is in the \"top layer\"\n * - The anchor and the surface do not share a common `position:relative`\n * ancestor\n */\n isTopLayer: boolean;\n /**\n * Whether or not the surface should be \"open\" and visible\n */\n isOpen: boolean;\n /**\n * The number of pixels in which to offset from the inline axis relative to\n * logical property.\n *\n * Positive is right in LTR and left in RTL.\n */\n xOffset: number;\n /**\n * The number of pixes in which to offset the block axis.\n *\n * Positive is down and negative is up.\n */\n yOffset: number;\n /**\n * A function to call after the surface has been positioned.\n */\n onOpen: () => void;\n /**\n * A function to call before the surface should be closed. (A good time to\n * perform animations while the surface is still visible)\n */\n beforeClose: () => Promise<void>;\n /**\n * A function to call after the surface has been closed.\n */\n onClose: () => void;\n}\n\n/**\n * Given a surface, an anchor, corners, and some options, this surface will\n * calculate the position of a surface to align the two given corners and keep\n * the surface inside the window viewport. It also provides a StyleInfo map that\n * can be applied to the surface to handle visiblility and position.\n */\nexport class SurfacePositionController implements ReactiveController {\n // The current styles to apply to the surface.\n private surfaceStylesInternal: StyleInfo = {\n 'display': 'none',\n };\n // Previous values stored for change detection. Open change detection is\n // calculated separately so initialize it here.\n private lastValues: SurfacePositionControllerProperties = {isOpen: false} as\n SurfacePositionControllerProperties;\n\n /**\n * @param host The host to connect the controller to.\n * @param getProperties A function that returns the properties for the\n * controller.\n */\n constructor(\n private readonly host: ReactiveControllerHost,\n private readonly getProperties: () => SurfacePositionControllerProperties,\n ) {\n this.host.addController(this);\n }\n\n /**\n * The StyleInfo map to apply to the surface via Lit's stylemap\n */\n get surfaceStyles() {\n return this.surfaceStylesInternal;\n }\n\n /**\n * Calculates the surface's new position required so that the surface's\n * `surfaceCorner` aligns to the anchor's `anchorCorner` while keeping the\n * surface inside the window viewport. This positioning also respects RTL by\n * checking `getComputedStyle()` on the surface element.\n */\n async position() {\n const {\n surfaceEl,\n anchorEl,\n anchorCorner: anchorCornerRaw,\n surfaceCorner: surfaceCornerRaw,\n isTopLayer: topLayerRaw,\n xOffset,\n yOffset,\n } = this.getProperties();\n const anchorCorner = anchorCornerRaw.toUpperCase().trim();\n const surfaceCorner = surfaceCornerRaw.toUpperCase().trim();\n\n if (!surfaceEl || !anchorEl) {\n return;\n }\n\n // Paint the surface transparently so that we can get the position and the\n // rect info of the surface.\n this.surfaceStylesInternal = {\n 'display': 'block',\n 'opacity': '0',\n };\n\n // Wait for it to be visible.\n this.host.requestUpdate();\n await this.host.updateComplete;\n\n const surfaceRect = surfaceEl.getBoundingClientRect();\n const anchorRect = anchorEl.getBoundingClientRect();\n const [surfaceBlock, surfaceInline] =\n surfaceCorner.split('_') as Array<'START'|'END'>;\n const [anchorBlock, anchorInline] =\n anchorCorner.split('_') as Array<'START'|'END'>;\n\n\n // We use number booleans to multiply values rather than `if` / ternary\n // statements because it _heavily_ cuts down on nesting and readability\n const isTopLayer = topLayerRaw ? 1 : 0;\n // LTR depends on the direction of the SURFACE not the anchor.\n const isLTR = getComputedStyle(surfaceEl).direction === 'ltr' ? 1 : 0;\n const isRTL = isLTR ? 0 : 1;\n const isSurfaceInlineStart = surfaceInline === 'START' ? 1 : 0;\n const isSurfaceInlineEnd = surfaceInline === 'END' ? 1 : 0;\n const isSurfaceBlockStart = surfaceBlock === 'START' ? 1 : 0;\n const isSurfaceBlockEnd = surfaceBlock === 'END' ? 1 : 0;\n const isOneInlineEnd = anchorInline !== surfaceInline ? 1 : 0;\n const isOneBlockEnd = anchorBlock !== surfaceBlock ? 1 : 0;\n\n /*\n * A diagram that helps describe some of the variables used in the following\n * calculations.\n *\n * ┌───── inline/blockTopLayerOffset\n * │ │\n * │ ┌─▼───┐ Window\n * │ ┌┼─────┴────────────────────────┐\n * │ ││ │\n * └──► ││ ┌──inline/blockAnchorOffset │\n * ││ │ │ │\n * └┤ │ ┌──▼───┐ │\n * │ │ ┌┼──────┤ │\n * │ └─►│Anchor│ │\n * │ └┴──────┘ │\n * │ │\n * │ ┌────────────────────────┼────┐\n * │ │ Surface │ │\n * │ │ │ │\n * │ │ │ │\n * │ │ │ │\n * │ │ │ │\n * │ │ │ │\n * └─────┼────────────────────────┘ ├┐\n * │ inline/blockOOBCorrection ││\n * │ │ ││\n * │ ├──►││\n * │ │ ││\n * └────────────────────────┐▼───┼┘\n * └────┘\n */\n\n // Whether or not to apply the width of the anchor\n const inlineAnchorOffset = isOneInlineEnd * anchorRect.width + xOffset;\n // The inline position of the anchor relative to window in LTR\n const inlineTopLayerOffsetLTR = isSurfaceInlineStart * anchorRect.left +\n isSurfaceInlineEnd * (window.innerWidth - anchorRect.right);\n // The inline position of the anchor relative to window in RTL\n const inlineTopLayerOffsetRTL =\n isSurfaceInlineStart * (window.innerWidth - anchorRect.right) +\n isSurfaceInlineEnd * anchorRect.left;\n // The inline position of the anchor relative to window\n const inlineTopLayerOffset =\n isLTR * inlineTopLayerOffsetLTR + isRTL * inlineTopLayerOffsetRTL;\n // If the surface's inline would be out of bounds of the window, move it\n // back in\n const inlineOutOfBoundsCorrection = Math.min(\n 0,\n window.innerWidth - inlineTopLayerOffset - inlineAnchorOffset -\n surfaceRect.width);\n\n // The inline logical value of the surface\n const inline = isTopLayer * inlineTopLayerOffset + inlineAnchorOffset +\n inlineOutOfBoundsCorrection;\n\n // Whether or not to apply the height of the anchor\n const blockAnchorOffset = isOneBlockEnd * anchorRect.height + yOffset;\n // The absolute block position of the anchor relative to window\n const blockTopLayerOffset = isSurfaceBlockStart * anchorRect.top +\n isSurfaceBlockEnd * (window.innerHeight - anchorRect.bottom);\n // If the surface's block would be out of bounds of the window, move it back\n // in\n const blockOutOfBoundsCorrection = Math.min(\n 0,\n window.innerHeight - blockTopLayerOffset - blockAnchorOffset -\n surfaceRect.height);\n\n // The block logical value of the surface\n const block = isTopLayer * blockTopLayerOffset + blockAnchorOffset +\n blockOutOfBoundsCorrection;\n\n const surfaceBlockProperty =\n surfaceBlock === 'START' ? 'inset-block-start' : 'inset-block-end';\n const surfaceInlineProperty =\n surfaceInline === 'START' ? 'inset-inline-start' : 'inset-inline-end';\n\n this.surfaceStylesInternal = {\n 'display': 'block',\n 'opacity': '1',\n [surfaceBlockProperty]: `${block}px`,\n [surfaceInlineProperty]: `${inline}px`,\n };\n\n this.host.requestUpdate();\n }\n\n hostUpdate() {\n this.onUpdate();\n }\n\n hostUpdated() {\n this.onUpdate();\n }\n\n /**\n * Checks whether the properties passed into the controller have changed since\n * the last positioning. If so, it will reposition if the surface is open or\n * close it if the surface should close.\n */\n private async onUpdate() {\n const props = this.getProperties();\n let hasChanged = false;\n for (const [key, value] of Object.entries(props)) {\n // tslint:disable-next-line\n hasChanged = hasChanged || (value !== (this.lastValues as any)[key]);\n if (hasChanged) break;\n }\n\n const openChanged = this.lastValues.isOpen !== props.isOpen;\n const hasAnchor = !!props.anchorEl;\n const hasSurface = !!props.surfaceEl;\n\n if (hasChanged && hasAnchor && hasSurface) {\n // Only update isOpen, because if it's closed, we do not want to waste\n // time on a useless reposition calculation. So save the other \"dirty\"\n // values until next time it opens.\n this.lastValues.isOpen = props.isOpen;\n\n if (props.isOpen) {\n // We are going to do a reposition, so save the prop values for future\n // dirty checking.\n this.lastValues = props;\n\n await this.position();\n props.onOpen();\n } else if (openChanged) {\n await props.beforeClose();\n this.close();\n props.onClose();\n }\n }\n }\n\n /**\n * Hides the surface.\n */\n private close() {\n this.surfaceStylesInternal = {\n 'display': 'none',\n };\n this.host.requestUpdate();\n }\n}\n"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { MenuItem } from './shared.js';
|
|
7
|
+
/**
|
|
8
|
+
* The options that are passed to the typeahead controller.
|
|
9
|
+
*/
|
|
10
|
+
export interface TypeaheadControllerProperties {
|
|
11
|
+
/**
|
|
12
|
+
* A function that returns an array of menu items to be searched.
|
|
13
|
+
* @return An array of menu items to be searched by typing.
|
|
14
|
+
*/
|
|
15
|
+
getItems: () => MenuItem[];
|
|
16
|
+
/**
|
|
17
|
+
* The maximum time between each keystroke to keep the current type buffer
|
|
18
|
+
* alive.
|
|
19
|
+
*/
|
|
20
|
+
typeaheadBufferTime: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Data structure tuple that helps with indexing.
|
|
24
|
+
*
|
|
25
|
+
* [index, item, normalized header text]
|
|
26
|
+
*/
|
|
27
|
+
declare type TypeaheadRecord = [number, MenuItem, string];
|
|
28
|
+
/**
|
|
29
|
+
* This controller listens to `keydown` events and searches the header text of
|
|
30
|
+
* an array of `MenuItem`s with the corresponding entered keys within the buffer
|
|
31
|
+
* time and activates the item.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const typeaheadController = new TypeaheadController(() => ({
|
|
36
|
+
* typeaheadBufferTime: 50,
|
|
37
|
+
* getItems: () => Array.from(document.querySelectorAll('md-menu-item'))
|
|
38
|
+
* }));
|
|
39
|
+
* html`
|
|
40
|
+
* <div
|
|
41
|
+
* @keydown=${typeaheadController.onKeydown}
|
|
42
|
+
* tabindex="0"
|
|
43
|
+
* class="activeItemText">
|
|
44
|
+
* <!-- focusable element that will receive keydown events -->
|
|
45
|
+
* Apple
|
|
46
|
+
* </div>
|
|
47
|
+
* <div>
|
|
48
|
+
* <md-menu-item active header="Apple"></md-menu-item>
|
|
49
|
+
* <md-menu-item header="Apricot"></md-menu-item>
|
|
50
|
+
* <md-menu-item header="Banana"></md-menu-item>
|
|
51
|
+
* <md-menu-item header="Olive"></md-menu-item>
|
|
52
|
+
* <md-menu-item header="Orange"></md-menu-item>
|
|
53
|
+
* </div>
|
|
54
|
+
* `;
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare class TypeaheadController {
|
|
58
|
+
protected getProperties: () => TypeaheadControllerProperties;
|
|
59
|
+
/**
|
|
60
|
+
* Array of tuples that helps with indexing.
|
|
61
|
+
*/
|
|
62
|
+
protected typeaheadRecords: TypeaheadRecord[];
|
|
63
|
+
/**
|
|
64
|
+
* Currently-typed text since last buffer timeout
|
|
65
|
+
*/
|
|
66
|
+
protected typaheadBuffer: string;
|
|
67
|
+
/**
|
|
68
|
+
* The timeout id from the current buffer's setTimeout
|
|
69
|
+
*/
|
|
70
|
+
protected cancelTypeaheadTimeout: number;
|
|
71
|
+
/**
|
|
72
|
+
* If we are currently "typing"
|
|
73
|
+
*/
|
|
74
|
+
protected isTypingAhead: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* The record of the last active item.
|
|
77
|
+
*/
|
|
78
|
+
protected lastActiveRecord: TypeaheadRecord | null;
|
|
79
|
+
/**
|
|
80
|
+
* @param getProperties A function that returns the options of the typeahead
|
|
81
|
+
* controller:
|
|
82
|
+
*
|
|
83
|
+
* {
|
|
84
|
+
* getItems: A function that returns an array of menu items to be searched.
|
|
85
|
+
* typeaheadBufferTime: The maximum time between each keystroke to keep the
|
|
86
|
+
* current type buffer alive.
|
|
87
|
+
* }
|
|
88
|
+
*/
|
|
89
|
+
constructor(getProperties: () => TypeaheadControllerProperties);
|
|
90
|
+
protected get items(): MenuItem[];
|
|
91
|
+
/**
|
|
92
|
+
* Apply this listener to the element that will receive `keydown` events that
|
|
93
|
+
* should trigger this controller.
|
|
94
|
+
*
|
|
95
|
+
* @param e The native browser `KeyboardEvent` from the `keydown` event.
|
|
96
|
+
*/
|
|
97
|
+
readonly onKeydown: (e: KeyboardEvent) => void;
|
|
98
|
+
/**
|
|
99
|
+
* Sets up typingahead
|
|
100
|
+
*/
|
|
101
|
+
protected beginTypeahead(e: KeyboardEvent): void;
|
|
102
|
+
/**
|
|
103
|
+
* Performs the typeahead. Based on the normalized items and the current text
|
|
104
|
+
* buffer, finds the _next_ item with matching text and activates it.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
*
|
|
108
|
+
* items: Apple, Banana, Olive, Orange, Cucumber
|
|
109
|
+
* buffer: ''
|
|
110
|
+
* user types: o
|
|
111
|
+
*
|
|
112
|
+
* activates Olive
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
*
|
|
116
|
+
* items: Apple, Banana, Olive (active), Orange, Cucumber
|
|
117
|
+
* buffer: 'o'
|
|
118
|
+
* user types: l
|
|
119
|
+
*
|
|
120
|
+
* activates Olive
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
*
|
|
124
|
+
* items: Apple, Banana, Olive (active), Orange, Cucumber
|
|
125
|
+
* buffer: ''
|
|
126
|
+
* user types: o
|
|
127
|
+
*
|
|
128
|
+
* activates Orange
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
*
|
|
132
|
+
* items: Apple, Banana, Olive, Orange (active), Cucumber
|
|
133
|
+
* buffer: ''
|
|
134
|
+
* user types: o
|
|
135
|
+
*
|
|
136
|
+
* activates Olive
|
|
137
|
+
*/
|
|
138
|
+
protected typeahead(e: KeyboardEvent): void;
|
|
139
|
+
/**
|
|
140
|
+
* Ends the current typeahead and clears the buffer.
|
|
141
|
+
*/
|
|
142
|
+
protected endTypeahead: () => void;
|
|
143
|
+
}
|
|
144
|
+
export {};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
// Indicies to access the TypeaheadRecord tuple
|
|
7
|
+
const TYPEAHEAD_INDEX = 0;
|
|
8
|
+
const TYPEAHEAD_ITEM = 1;
|
|
9
|
+
const TYPEAHEAD_TEXT = 2;
|
|
10
|
+
/**
|
|
11
|
+
* This controller listens to `keydown` events and searches the header text of
|
|
12
|
+
* an array of `MenuItem`s with the corresponding entered keys within the buffer
|
|
13
|
+
* time and activates the item.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const typeaheadController = new TypeaheadController(() => ({
|
|
18
|
+
* typeaheadBufferTime: 50,
|
|
19
|
+
* getItems: () => Array.from(document.querySelectorAll('md-menu-item'))
|
|
20
|
+
* }));
|
|
21
|
+
* html`
|
|
22
|
+
* <div
|
|
23
|
+
* @keydown=${typeaheadController.onKeydown}
|
|
24
|
+
* tabindex="0"
|
|
25
|
+
* class="activeItemText">
|
|
26
|
+
* <!-- focusable element that will receive keydown events -->
|
|
27
|
+
* Apple
|
|
28
|
+
* </div>
|
|
29
|
+
* <div>
|
|
30
|
+
* <md-menu-item active header="Apple"></md-menu-item>
|
|
31
|
+
* <md-menu-item header="Apricot"></md-menu-item>
|
|
32
|
+
* <md-menu-item header="Banana"></md-menu-item>
|
|
33
|
+
* <md-menu-item header="Olive"></md-menu-item>
|
|
34
|
+
* <md-menu-item header="Orange"></md-menu-item>
|
|
35
|
+
* </div>
|
|
36
|
+
* `;
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class TypeaheadController {
|
|
40
|
+
/**
|
|
41
|
+
* @param getProperties A function that returns the options of the typeahead
|
|
42
|
+
* controller:
|
|
43
|
+
*
|
|
44
|
+
* {
|
|
45
|
+
* getItems: A function that returns an array of menu items to be searched.
|
|
46
|
+
* typeaheadBufferTime: The maximum time between each keystroke to keep the
|
|
47
|
+
* current type buffer alive.
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
50
|
+
constructor(getProperties) {
|
|
51
|
+
this.getProperties = getProperties;
|
|
52
|
+
/**
|
|
53
|
+
* Array of tuples that helps with indexing.
|
|
54
|
+
*/
|
|
55
|
+
this.typeaheadRecords = [];
|
|
56
|
+
/**
|
|
57
|
+
* Currently-typed text since last buffer timeout
|
|
58
|
+
*/
|
|
59
|
+
this.typaheadBuffer = '';
|
|
60
|
+
/**
|
|
61
|
+
* The timeout id from the current buffer's setTimeout
|
|
62
|
+
*/
|
|
63
|
+
this.cancelTypeaheadTimeout = 0;
|
|
64
|
+
/**
|
|
65
|
+
* If we are currently "typing"
|
|
66
|
+
*/
|
|
67
|
+
this.isTypingAhead = false;
|
|
68
|
+
/**
|
|
69
|
+
* The record of the last active item.
|
|
70
|
+
*/
|
|
71
|
+
this.lastActiveRecord = null;
|
|
72
|
+
/**
|
|
73
|
+
* Apply this listener to the element that will receive `keydown` events that
|
|
74
|
+
* should trigger this controller.
|
|
75
|
+
*
|
|
76
|
+
* @param e The native browser `KeyboardEvent` from the `keydown` event.
|
|
77
|
+
*/
|
|
78
|
+
this.onKeydown = (e) => {
|
|
79
|
+
if (this.isTypingAhead) {
|
|
80
|
+
this.typeahead(e);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.beginTypeahead(e);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Ends the current typeahead and clears the buffer.
|
|
88
|
+
*/
|
|
89
|
+
this.endTypeahead = () => {
|
|
90
|
+
this.isTypingAhead = false;
|
|
91
|
+
this.typaheadBuffer = '';
|
|
92
|
+
this.typeaheadRecords = [];
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
get items() {
|
|
96
|
+
return this.getProperties().getItems();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Sets up typingahead
|
|
100
|
+
*/
|
|
101
|
+
beginTypeahead(e) {
|
|
102
|
+
// We don't want to typeahead if the _beginning_ of the typeahead is a menu
|
|
103
|
+
// navigation, or a selection. We will handle "Space" only if it's in the
|
|
104
|
+
// middle of a typeahead
|
|
105
|
+
if (e.code === 'Space' || e.code === 'Enter' ||
|
|
106
|
+
e.code.startsWith('Arrow') || e.code === 'Escape') {
|
|
107
|
+
if (this.lastActiveRecord) {
|
|
108
|
+
this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.isTypingAhead = true;
|
|
113
|
+
// Generates the record array data structure which is the index, the element
|
|
114
|
+
// and a normalized header.
|
|
115
|
+
this.typeaheadRecords = this.items.map((el, index) => [index, el, el.headline.trim().toLowerCase()]);
|
|
116
|
+
this.lastActiveRecord =
|
|
117
|
+
this.typeaheadRecords.find(record => record[TYPEAHEAD_ITEM].active) ??
|
|
118
|
+
null;
|
|
119
|
+
if (this.lastActiveRecord) {
|
|
120
|
+
this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;
|
|
121
|
+
}
|
|
122
|
+
this.typeahead(e);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Performs the typeahead. Based on the normalized items and the current text
|
|
126
|
+
* buffer, finds the _next_ item with matching text and activates it.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
*
|
|
130
|
+
* items: Apple, Banana, Olive, Orange, Cucumber
|
|
131
|
+
* buffer: ''
|
|
132
|
+
* user types: o
|
|
133
|
+
*
|
|
134
|
+
* activates Olive
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
*
|
|
138
|
+
* items: Apple, Banana, Olive (active), Orange, Cucumber
|
|
139
|
+
* buffer: 'o'
|
|
140
|
+
* user types: l
|
|
141
|
+
*
|
|
142
|
+
* activates Olive
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
*
|
|
146
|
+
* items: Apple, Banana, Olive (active), Orange, Cucumber
|
|
147
|
+
* buffer: ''
|
|
148
|
+
* user types: o
|
|
149
|
+
*
|
|
150
|
+
* activates Orange
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
*
|
|
154
|
+
* items: Apple, Banana, Olive, Orange (active), Cucumber
|
|
155
|
+
* buffer: ''
|
|
156
|
+
* user types: o
|
|
157
|
+
*
|
|
158
|
+
* activates Olive
|
|
159
|
+
*/
|
|
160
|
+
typeahead(e) {
|
|
161
|
+
clearTimeout(this.cancelTypeaheadTimeout);
|
|
162
|
+
// Stop typingahead if one of the navigation or selection keys (except for
|
|
163
|
+
// Space) are pressed
|
|
164
|
+
if (e.code === 'Enter' || e.code.startsWith('Arrow') ||
|
|
165
|
+
e.code === 'Escape') {
|
|
166
|
+
this.endTypeahead();
|
|
167
|
+
if (this.lastActiveRecord) {
|
|
168
|
+
this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
// If Space is pressed, prevent it from selecting and closing the menu
|
|
173
|
+
if (e.code === 'Space') {
|
|
174
|
+
e.stopPropagation();
|
|
175
|
+
}
|
|
176
|
+
// Start up a new keystroke buffer timeout
|
|
177
|
+
this.cancelTypeaheadTimeout =
|
|
178
|
+
setTimeout(this.endTypeahead, this.getProperties().typeaheadBufferTime);
|
|
179
|
+
this.typaheadBuffer += e.key.toLowerCase();
|
|
180
|
+
const lastActiveIndex = this.lastActiveRecord ? this.lastActiveRecord[TYPEAHEAD_INDEX] : -1;
|
|
181
|
+
const numRecords = this.typeaheadRecords.length;
|
|
182
|
+
/**
|
|
183
|
+
* Sorting function that will resort the items starting with the given index
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
*
|
|
187
|
+
* this.typeaheadRecords =
|
|
188
|
+
* 0: [0, <reference>, 'apple']
|
|
189
|
+
* 1: [1, <reference>, 'apricot']
|
|
190
|
+
* 2: [2, <reference>, 'banana']
|
|
191
|
+
* 3: [3, <reference>, 'olive'] <-- lastActiveIndex
|
|
192
|
+
* 4: [4, <reference>, 'orange']
|
|
193
|
+
* 5: [5, <reference>, 'strawberry']
|
|
194
|
+
*
|
|
195
|
+
* this.typeaheadRecords.sort((a,b) => rebaseIndexOnActive(a)
|
|
196
|
+
* - rebaseIndexOnActive(b)) ===
|
|
197
|
+
* 0: [3, <reference>, 'olive'] <-- lastActiveIndex
|
|
198
|
+
* 1: [4, <reference>, 'orange']
|
|
199
|
+
* 2: [5, <reference>, 'strawberry']
|
|
200
|
+
* 3: [0, <reference>, 'apple']
|
|
201
|
+
* 4: [1, <reference>, 'apricot']
|
|
202
|
+
* 5: [2, <reference>, 'banana']
|
|
203
|
+
*/
|
|
204
|
+
const rebaseIndexOnActive = (record) => {
|
|
205
|
+
return (record[TYPEAHEAD_INDEX] + numRecords - lastActiveIndex) %
|
|
206
|
+
numRecords;
|
|
207
|
+
};
|
|
208
|
+
// records filtered and sorted / rebased around the last active index
|
|
209
|
+
const matchingRecords = this.typeaheadRecords
|
|
210
|
+
.filter(record => !record[TYPEAHEAD_ITEM].disabled &&
|
|
211
|
+
record[TYPEAHEAD_TEXT].startsWith(this.typaheadBuffer))
|
|
212
|
+
.sort((a, b) => rebaseIndexOnActive(a) - rebaseIndexOnActive(b));
|
|
213
|
+
// Just leave if there's nothing that matches. Native select will just
|
|
214
|
+
// choose the first thing that starts with the next letter in the alphabet
|
|
215
|
+
// but that's out of scope and hard to localize
|
|
216
|
+
if (matchingRecords.length === 0) {
|
|
217
|
+
clearTimeout(this.cancelTypeaheadTimeout);
|
|
218
|
+
if (this.lastActiveRecord) {
|
|
219
|
+
this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;
|
|
220
|
+
}
|
|
221
|
+
this.endTypeahead();
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const isNewQuery = this.typaheadBuffer.length === 1;
|
|
225
|
+
let nextRecord;
|
|
226
|
+
// This is likely the case that someone is trying to "tab" through different
|
|
227
|
+
// entries that start with the same letter
|
|
228
|
+
if (this.lastActiveRecord === matchingRecords[0] && isNewQuery) {
|
|
229
|
+
nextRecord = matchingRecords[1] ?? matchingRecords[0];
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
nextRecord = matchingRecords[0];
|
|
233
|
+
}
|
|
234
|
+
if (this.lastActiveRecord) {
|
|
235
|
+
this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;
|
|
236
|
+
}
|
|
237
|
+
this.lastActiveRecord = nextRecord;
|
|
238
|
+
nextRecord[TYPEAHEAD_ITEM].active = true;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=typeaheadController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeaheadController.js","sourceRoot":"","sources":["typeaheadController.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA0BH,+CAA+C;AAC/C,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,cAAc,GAAG,CAAC,CAAC;AACzB,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,OAAO,mBAAmB;IAsB9B;;;;;;;;;OASG;IACH,YACc,aAAkD;QAAlD,kBAAa,GAA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AK,CAAC,CAAC;QACpD,IAAI,UAA2B,CAAC;QAEhC,4EAA4E;QAC5E,0CAA0C;QAC1C,IAAI,IAAI,CAAC,gBAAgB,KAAK,eAAe,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE;YAC9D,UAAU,GAAG,eAAe,CAAC,CAAC,CAAC,IAAI,eAAe,CAAC,CAAC,CAAC,CAAC;SACvD;aAAM;YACL,UAAU,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;SACjC;QAED,IAAI,IAAI,CAAC,gBAAgB,EAAE;YACzB,IAAI,CAAC,gBAAgB,CAAC,cAAc,CAAC,CAAC,MAAM,GAAG,KAAK,CAAC;SACtD;QAED,IAAI,CAAC,gBAAgB,GAAG,UAAU,CAAC;QACnC,UAAU,CAAC,cAAc,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC;QACzC,OAAO;IACT,CAAC;CAUF","sourcesContent":["/**\n * @license\n * Copyright 2023 Google LLC\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {MenuItem} from './shared.js';\n\n/**\n * The options that are passed to the typeahead controller.\n */\nexport interface TypeaheadControllerProperties {\n /**\n * A function that returns an array of menu items to be searched.\n * @return An array of menu items to be searched by typing.\n */\n getItems: () => MenuItem[];\n /**\n * The maximum time between each keystroke to keep the current type buffer\n * alive.\n */\n typeaheadBufferTime: number;\n}\n\n/**\n * Data structure tuple that helps with indexing.\n *\n * [index, item, normalized header text]\n */\ntype TypeaheadRecord = [number, MenuItem, string];\n// Indicies to access the TypeaheadRecord tuple\nconst TYPEAHEAD_INDEX = 0;\nconst TYPEAHEAD_ITEM = 1;\nconst TYPEAHEAD_TEXT = 2;\n\n/**\n * This controller listens to `keydown` events and searches the header text of\n * an array of `MenuItem`s with the corresponding entered keys within the buffer\n * time and activates the item.\n *\n * @example\n * ```ts\n * const typeaheadController = new TypeaheadController(() => ({\n * typeaheadBufferTime: 50,\n * getItems: () => Array.from(document.querySelectorAll('md-menu-item'))\n * }));\n * html`\n * <div\n * @keydown=${typeaheadController.onKeydown}\n * tabindex=\"0\"\n * class=\"activeItemText\">\n * <!-- focusable element that will receive keydown events -->\n * Apple\n * </div>\n * <div>\n * <md-menu-item active header=\"Apple\"></md-menu-item>\n * <md-menu-item header=\"Apricot\"></md-menu-item>\n * <md-menu-item header=\"Banana\"></md-menu-item>\n * <md-menu-item header=\"Olive\"></md-menu-item>\n * <md-menu-item header=\"Orange\"></md-menu-item>\n * </div>\n * `;\n * ```\n */\nexport class TypeaheadController {\n /**\n * Array of tuples that helps with indexing.\n */\n protected typeaheadRecords: TypeaheadRecord[] = [];\n /**\n * Currently-typed text since last buffer timeout\n */\n protected typaheadBuffer = '';\n /**\n * The timeout id from the current buffer's setTimeout\n */\n protected cancelTypeaheadTimeout = 0;\n /**\n * If we are currently \"typing\"\n */\n protected isTypingAhead = false;\n /**\n * The record of the last active item.\n */\n protected lastActiveRecord: TypeaheadRecord|null = null;\n\n /**\n * @param getProperties A function that returns the options of the typeahead\n * controller:\n *\n * {\n * getItems: A function that returns an array of menu items to be searched.\n * typeaheadBufferTime: The maximum time between each keystroke to keep the\n * current type buffer alive.\n * }\n */\n constructor(\n protected getProperties: () => TypeaheadControllerProperties,\n ) {}\n\n protected get items() {\n return this.getProperties().getItems();\n }\n\n /**\n * Apply this listener to the element that will receive `keydown` events that\n * should trigger this controller.\n *\n * @param e The native browser `KeyboardEvent` from the `keydown` event.\n */\n readonly onKeydown = (e: KeyboardEvent) => {\n if (this.isTypingAhead) {\n this.typeahead(e);\n } else {\n this.beginTypeahead(e);\n }\n };\n\n /**\n * Sets up typingahead\n */\n protected beginTypeahead(e: KeyboardEvent) {\n // We don't want to typeahead if the _beginning_ of the typeahead is a menu\n // navigation, or a selection. We will handle \"Space\" only if it's in the\n // middle of a typeahead\n if (e.code === 'Space' || e.code === 'Enter' ||\n e.code.startsWith('Arrow') || e.code === 'Escape') {\n if (this.lastActiveRecord) {\n this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;\n }\n return;\n }\n\n this.isTypingAhead = true;\n // Generates the record array data structure which is the index, the element\n // and a normalized header.\n this.typeaheadRecords = this.items.map(\n (el, index) => [index, el, el.headline.trim().toLowerCase()]);\n this.lastActiveRecord =\n this.typeaheadRecords.find(record => record[TYPEAHEAD_ITEM].active) ??\n null;\n if (this.lastActiveRecord) {\n this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;\n }\n this.typeahead(e);\n }\n\n /**\n * Performs the typeahead. Based on the normalized items and the current text\n * buffer, finds the _next_ item with matching text and activates it.\n *\n * @example\n *\n * items: Apple, Banana, Olive, Orange, Cucumber\n * buffer: ''\n * user types: o\n *\n * activates Olive\n *\n * @example\n *\n * items: Apple, Banana, Olive (active), Orange, Cucumber\n * buffer: 'o'\n * user types: l\n *\n * activates Olive\n *\n * @example\n *\n * items: Apple, Banana, Olive (active), Orange, Cucumber\n * buffer: ''\n * user types: o\n *\n * activates Orange\n *\n * @example\n *\n * items: Apple, Banana, Olive, Orange (active), Cucumber\n * buffer: ''\n * user types: o\n *\n * activates Olive\n */\n protected typeahead(e: KeyboardEvent) {\n clearTimeout(this.cancelTypeaheadTimeout);\n // Stop typingahead if one of the navigation or selection keys (except for\n // Space) are pressed\n if (e.code === 'Enter' || e.code.startsWith('Arrow') ||\n e.code === 'Escape') {\n this.endTypeahead();\n if (this.lastActiveRecord) {\n this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;\n }\n return;\n }\n\n // If Space is pressed, prevent it from selecting and closing the menu\n if (e.code === 'Space') {\n e.stopPropagation();\n }\n\n // Start up a new keystroke buffer timeout\n this.cancelTypeaheadTimeout =\n setTimeout(this.endTypeahead, this.getProperties().typeaheadBufferTime);\n\n this.typaheadBuffer += e.key.toLowerCase();\n\n const lastActiveIndex =\n this.lastActiveRecord ? this.lastActiveRecord[TYPEAHEAD_INDEX] : -1;\n const numRecords = this.typeaheadRecords.length;\n\n /**\n * Sorting function that will resort the items starting with the given index\n *\n * @example\n * \n * this.typeaheadRecords = \n * 0: [0, <reference>, 'apple']\n * 1: [1, <reference>, 'apricot']\n * 2: [2, <reference>, 'banana']\n * 3: [3, <reference>, 'olive'] <-- lastActiveIndex\n * 4: [4, <reference>, 'orange']\n * 5: [5, <reference>, 'strawberry']\n * \n * this.typeaheadRecords.sort((a,b) => rebaseIndexOnActive(a)\n * - rebaseIndexOnActive(b)) ===\n * 0: [3, <reference>, 'olive'] <-- lastActiveIndex\n * 1: [4, <reference>, 'orange']\n * 2: [5, <reference>, 'strawberry']\n * 3: [0, <reference>, 'apple']\n * 4: [1, <reference>, 'apricot']\n * 5: [2, <reference>, 'banana']\n */\n const rebaseIndexOnActive = (record: TypeaheadRecord) => {\n return (record[TYPEAHEAD_INDEX] + numRecords - lastActiveIndex) %\n numRecords;\n };\n\n // records filtered and sorted / rebased around the last active index\n const matchingRecords =\n this.typeaheadRecords\n .filter(\n record => !record[TYPEAHEAD_ITEM].disabled &&\n record[TYPEAHEAD_TEXT].startsWith(this.typaheadBuffer))\n .sort(\n (a, b) => rebaseIndexOnActive(a) - rebaseIndexOnActive(b));\n\n // Just leave if there's nothing that matches. Native select will just\n // choose the first thing that starts with the next letter in the alphabet\n // but that's out of scope and hard to localize\n if (matchingRecords.length === 0) {\n clearTimeout(this.cancelTypeaheadTimeout);\n if (this.lastActiveRecord) {\n this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;\n }\n this.endTypeahead();\n return;\n }\n\n const isNewQuery = this.typaheadBuffer.length === 1;\n let nextRecord: TypeaheadRecord;\n\n // This is likely the case that someone is trying to \"tab\" through different\n // entries that start with the same letter\n if (this.lastActiveRecord === matchingRecords[0] && isNewQuery) {\n nextRecord = matchingRecords[1] ?? matchingRecords[0];\n } else {\n nextRecord = matchingRecords[0];\n }\n\n if (this.lastActiveRecord) {\n this.lastActiveRecord[TYPEAHEAD_ITEM].active = false;\n }\n\n this.lastActiveRecord = nextRecord;\n nextRecord[TYPEAHEAD_ITEM].active = true;\n return;\n }\n\n /**\n * Ends the current typeahead and clears the buffer.\n */\n protected endTypeahead = () => {\n this.isTypingAhead = false;\n this.typaheadBuffer = '';\n this.typeaheadRecords = [];\n };\n}\n"]}
|