@momentum-design/components 0.22.7 → 0.23.0

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.
@@ -0,0 +1,7 @@
1
+ import VirtualizedList from './virtualizedlist.component';
2
+ declare global {
3
+ interface HTMLElementTagNameMap {
4
+ ['mdc-virtualizedlist']: VirtualizedList;
5
+ }
6
+ }
7
+ export default VirtualizedList;
@@ -0,0 +1,4 @@
1
+ import VirtualizedList from './virtualizedlist.component';
2
+ import { TAG_NAME } from './virtualizedlist.constants';
3
+ VirtualizedList.register(TAG_NAME);
4
+ export default VirtualizedList;
@@ -0,0 +1,79 @@
1
+ import { CSSResult, PropertyValues, TemplateResult } from 'lit';
2
+ import { Virtualizer } from '@tanstack/virtual-core';
3
+ import { Ref } from 'lit/directives/ref.js';
4
+ import { Component } from '../../models';
5
+ import { SetListDataProps, VirtualizerProps } from './virtualizedlist.types';
6
+ /**
7
+ * `mdc-virtualizedlist` component for creating custom virtualized lists.
8
+ * IMPORTANT: This component does not create it's own list/list items.
9
+ * Use the setlistdata callback prop to update client state in order to
10
+ * Pass list/listitems as a child of this component, which this will virtuailze
11
+ * This implementation handles dynamic lists as well as fixed sized lists.
12
+ * Please refer to [Tanstack Virtual Docs](https://tanstack.com/virtual/latest) for more in depth documentation.
13
+ *
14
+ * @tagname mdc-virtualizedlist
15
+ *
16
+ * @slot - Client side List with nested list items.
17
+ */
18
+ declare class VirtualizedList extends Component {
19
+ /**
20
+ * Callback that gets called when user scrolls inside of list. This gives access to the scroll container element
21
+ * as well via the event. Particularly useful for
22
+ * handling logic related when the user scrolls to the top or bottom of a list.
23
+ * @default undefined
24
+ */
25
+ onscroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
26
+ /**
27
+ * Object that sets and updates the virtualizer with any relevant props.
28
+ * There are two required object props in order to get virtualization to work properly.
29
+ * count - The length of your list that you are virtualizing.
30
+ * As your list grows/shrinks, this component must be updated with the appropriate value
31
+ * (Same with any other updated prop).
32
+ * estimateSize - A function that returns the estimated size of your items.
33
+ * If your list is fixed, this will just be the size of your items.
34
+ * If your list is dynamic, try to return approximate the size of each item.
35
+ *
36
+ * A full list of possible props can be in
37
+ * [Tanstack Virtualizer API Docs](https://tanstack.com/virtual/latest/docs/api/virtualizer)
38
+ *
39
+ */
40
+ virtualizerprops: VirtualizerProps;
41
+ /**
42
+ * Callback that gets envoked when updates to the virtualizer interally occur.
43
+ * This must be implemented in such a way that this function will trigger update to parent.
44
+ *
45
+ * virtualItems - Array that will be what the client displays on screen. Use this to render
46
+ * a List of your choosing with these items nested inside as your ListItems.
47
+ * measureElement - Ref to pass to each ListItem rendered client side.
48
+ * Each ListItem should also be be passed key and a data-index (which can be found on the virtualItem).
49
+ * listStyle - This should be passed as the style attribute to your List.
50
+ */
51
+ setlistdata: (({ virtualItems, measureElement, listStyle }: SetListDataProps) => void) | null;
52
+ /**
53
+ * @internal
54
+ */
55
+ private virtualizerController;
56
+ scrollElementRef: Ref<HTMLDivElement>;
57
+ virtualizer: Virtualizer<Element, Element> | null;
58
+ constructor();
59
+ /**
60
+ * This override is necessary to update the virtualizer with relevant props
61
+ * if the client updates any props (most commonly, count). Updating the options
62
+ * this way ensures we don't initialize a new virtualizer upon very prop change.
63
+ */
64
+ update(changedProperties: PropertyValues): void;
65
+ connectedCallback(): void;
66
+ /**
67
+ * @internal
68
+ * Renders the list wrapper and invokes the callback which eventually will render in the slot.
69
+ * Uses getTotalSize to update the height of the wrapper. This value is equal to the total size
70
+ * OR the total estimated size (if you haven't physically scrolled the entire list)
71
+ * Passes the virtualItems, measureElement, and listStyle to callback for client to pass in as child
72
+ *
73
+ * @returns The template result containing the list wrapper.
74
+ */
75
+ private getVirtualizedListWrapper;
76
+ render(): TemplateResult<1>;
77
+ static styles: Array<CSSResult>;
78
+ }
79
+ export default VirtualizedList;
@@ -0,0 +1,135 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { html } from 'lit';
11
+ import { VirtualizerController } from '@tanstack/lit-virtual';
12
+ import { property } from 'lit/decorators.js';
13
+ import { createRef, ref } from 'lit/directives/ref.js';
14
+ import styles from './virtualizedlist.styles';
15
+ import { Component } from '../../models';
16
+ import { DEFAULTS } from './virtualizedlist.constants';
17
+ /**
18
+ * `mdc-virtualizedlist` component for creating custom virtualized lists.
19
+ * IMPORTANT: This component does not create it's own list/list items.
20
+ * Use the setlistdata callback prop to update client state in order to
21
+ * Pass list/listitems as a child of this component, which this will virtuailze
22
+ * This implementation handles dynamic lists as well as fixed sized lists.
23
+ * Please refer to [Tanstack Virtual Docs](https://tanstack.com/virtual/latest) for more in depth documentation.
24
+ *
25
+ * @tagname mdc-virtualizedlist
26
+ *
27
+ * @slot - Client side List with nested list items.
28
+ */
29
+ class VirtualizedList extends Component {
30
+ constructor() {
31
+ super();
32
+ /**
33
+ * Object that sets and updates the virtualizer with any relevant props.
34
+ * There are two required object props in order to get virtualization to work properly.
35
+ * count - The length of your list that you are virtualizing.
36
+ * As your list grows/shrinks, this component must be updated with the appropriate value
37
+ * (Same with any other updated prop).
38
+ * estimateSize - A function that returns the estimated size of your items.
39
+ * If your list is fixed, this will just be the size of your items.
40
+ * If your list is dynamic, try to return approximate the size of each item.
41
+ *
42
+ * A full list of possible props can be in
43
+ * [Tanstack Virtualizer API Docs](https://tanstack.com/virtual/latest/docs/api/virtualizer)
44
+ *
45
+ */
46
+ this.virtualizerprops = DEFAULTS.VIRTUALIZER_PROPS;
47
+ this.scrollElementRef = createRef();
48
+ this.virtualizerController = null;
49
+ this.virtualizer = null;
50
+ this.setlistdata = null;
51
+ this.onscroll = null;
52
+ }
53
+ /**
54
+ * This override is necessary to update the virtualizer with relevant props
55
+ * if the client updates any props (most commonly, count). Updating the options
56
+ * this way ensures we don't initialize a new virtualizer upon very prop change.
57
+ */
58
+ update(changedProperties) {
59
+ var _a;
60
+ super.update(changedProperties);
61
+ // if the virtuailzer props change at all,
62
+ // update virtuailzer with the union of the two virtualizer options (current, passed in).
63
+ if (changedProperties.get('virtualizerprops')) {
64
+ (_a = this.virtualizer) === null || _a === void 0 ? void 0 : _a.setOptions({ ...this.virtualizer.options, ...this.virtualizerprops });
65
+ this.requestUpdate();
66
+ }
67
+ }
68
+ connectedCallback() {
69
+ var _a;
70
+ this.virtualizerController = new VirtualizerController(this, {
71
+ count: this.virtualizerprops.count,
72
+ estimateSize: (_a = this.virtualizerprops) === null || _a === void 0 ? void 0 : _a.estimateSize,
73
+ getScrollElement: () => this.scrollElementRef.value || null,
74
+ ...this.virtualizerprops,
75
+ });
76
+ super.connectedCallback();
77
+ }
78
+ /**
79
+ * @internal
80
+ * Renders the list wrapper and invokes the callback which eventually will render in the slot.
81
+ * Uses getTotalSize to update the height of the wrapper. This value is equal to the total size
82
+ * OR the total estimated size (if you haven't physically scrolled the entire list)
83
+ * Passes the virtualItems, measureElement, and listStyle to callback for client to pass in as child
84
+ *
85
+ * @returns The template result containing the list wrapper.
86
+ */
87
+ getVirtualizedListWrapper(virtualizerController) {
88
+ var _a, _b;
89
+ this.virtualizer = virtualizerController.getVirtualizer();
90
+ const { getVirtualItems, measureElement, getTotalSize } = this.virtualizer;
91
+ const virtualItems = getVirtualItems();
92
+ // this style is required to be rendered by the client side list in order to handle scrolling properly
93
+ const listStyle = {
94
+ position: 'absolute',
95
+ top: 0,
96
+ left: 0,
97
+ width: '100%',
98
+ transform: `translateY(${(_b = (_a = virtualItems[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : 0}px)`,
99
+ };
100
+ // pass back data to client for rendering
101
+ if (this.setlistdata) {
102
+ this.setlistdata({ virtualItems, measureElement, listStyle });
103
+ }
104
+ return html `<div
105
+ class="mdc-virtualizedlist-container"
106
+ style="height: ${getTotalSize()}px;"
107
+ >
108
+ <slot></slot>
109
+ </div>`;
110
+ }
111
+ render() {
112
+ return html `<div
113
+ ${ref(this.scrollElementRef)}
114
+ class="mdc-virtualizedlist-scroll"
115
+ @scroll=${this.onscroll && this.onscroll}
116
+ >
117
+ ${this.virtualizerController ? this.getVirtualizedListWrapper(this.virtualizerController) : html ``}
118
+ </div>
119
+ `;
120
+ }
121
+ }
122
+ VirtualizedList.styles = [...Component.styles, ...styles];
123
+ __decorate([
124
+ property({ type: Function, attribute: 'onscroll' }),
125
+ __metadata("design:type", Object)
126
+ ], VirtualizedList.prototype, "onscroll", void 0);
127
+ __decorate([
128
+ property({ type: Object, attribute: 'virtualizerprops' }),
129
+ __metadata("design:type", Object)
130
+ ], VirtualizedList.prototype, "virtualizerprops", void 0);
131
+ __decorate([
132
+ property({ type: Function, attribute: 'setlistdata' }),
133
+ __metadata("design:type", Object)
134
+ ], VirtualizedList.prototype, "setlistdata", void 0);
135
+ export default VirtualizedList;
@@ -0,0 +1,9 @@
1
+ declare const TAG_NAME: "mdc-virtualizedlist";
2
+ declare const VIRTUALIZED_WRAPPER_TAG_NAME: "mdc-virtualizedwrapper";
3
+ declare const DEFAULTS: {
4
+ VIRTUALIZER_PROPS: {
5
+ count: number;
6
+ estimateSize: () => number;
7
+ };
8
+ };
9
+ export { TAG_NAME, DEFAULTS, VIRTUALIZED_WRAPPER_TAG_NAME };
@@ -0,0 +1,10 @@
1
+ import utils from '../../utils/tag-name';
2
+ const TAG_NAME = utils.constructTagName('virtualizedlist');
3
+ const VIRTUALIZED_WRAPPER_TAG_NAME = utils.constructTagName('virtualizedwrapper');
4
+ const DEFAULTS = {
5
+ VIRTUALIZER_PROPS: {
6
+ count: 0,
7
+ estimateSize: () => 0,
8
+ },
9
+ };
10
+ export { TAG_NAME, DEFAULTS, VIRTUALIZED_WRAPPER_TAG_NAME };
@@ -0,0 +1,22 @@
1
+ import { CSSResult, PropertyValues, TemplateResult } from 'lit';
2
+ import { VirtualizerProps } from './virtualizedlist.types';
3
+ import { Component } from '../../models';
4
+ declare class VirtualizedWrapper extends Component {
5
+ onscroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
6
+ virtualizerprops: VirtualizerProps;
7
+ list: TemplateResult<1>;
8
+ listItemTexts: string[];
9
+ constructor();
10
+ update(changedProperties: PropertyValues): void;
11
+ connectedCallback(): void;
12
+ private updateListItemTextArray;
13
+ private setListData;
14
+ render(): TemplateResult<1>;
15
+ static styles: Array<CSSResult>;
16
+ }
17
+ declare global {
18
+ interface HTMLElementTagNameMap {
19
+ ['mdc-virtualizedwrapper']: VirtualizedWrapper;
20
+ }
21
+ }
22
+ export {};
@@ -0,0 +1,79 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { html } from 'lit';
11
+ import { property, state } from 'lit/decorators.js';
12
+ import { ref } from 'lit/directives/ref.js';
13
+ import { styleMap } from 'lit/directives/style-map.js';
14
+ import { Component } from '../../models';
15
+ class VirtualizedWrapper extends Component {
16
+ constructor() {
17
+ super();
18
+ this.virtualizerprops = { count: 100, estimateSize: () => 100, overscan: 60 };
19
+ this.list = html ``;
20
+ this.listItemTexts = new Array(this.virtualizerprops.count)
21
+ .fill(true)
22
+ .map((_, index) => `list item number ${index}`);
23
+ this.onscroll = null;
24
+ this.setListData = this.setListData.bind(this);
25
+ }
26
+ update(changedProperties) {
27
+ super.update(changedProperties);
28
+ if (changedProperties.get('virtualizerprops')) {
29
+ this.updateListItemTextArray();
30
+ }
31
+ }
32
+ connectedCallback() {
33
+ var _a;
34
+ super.connectedCallback();
35
+ if ((_a = this.virtualizerprops) === null || _a === void 0 ? void 0 : _a.count) {
36
+ this.updateListItemTextArray();
37
+ }
38
+ }
39
+ updateListItemTextArray() {
40
+ var _a;
41
+ this.listItemTexts = new Array((_a = this.virtualizerprops) === null || _a === void 0 ? void 0 : _a.count)
42
+ .fill(true)
43
+ .map((_, index) => `list item number ${index}`);
44
+ }
45
+ setListData({ virtualItems, measureElement, listStyle }) {
46
+ if (virtualItems) {
47
+ this.list = html `<ul style="margin: 0;${styleMap(listStyle)}">${virtualItems.map((virtualItem) => html `<li role="listitem" key=${virtualItem.key} data-index=${virtualItem.index} ref=${ref(measureElement)}>
48
+ ${this.listItemTexts[virtualItem.index]}</li>`)}</ul>`;
49
+ }
50
+ }
51
+ render() {
52
+ return html `
53
+ <div style="height: 500px; width: 500px;">
54
+ <mdc-virtualizedlist
55
+ .onscroll=${this.onscroll}
56
+ .virtualizerprops=${this.virtualizerprops}
57
+ .setlistdata=${this.setListData}
58
+ >${this.list}</mdc-virtualizedlist></div>
59
+ `;
60
+ }
61
+ }
62
+ VirtualizedWrapper.styles = Component.styles;
63
+ __decorate([
64
+ property({ type: Function, attribute: 'onscroll' }),
65
+ __metadata("design:type", Object)
66
+ ], VirtualizedWrapper.prototype, "onscroll", void 0);
67
+ __decorate([
68
+ property({ type: Object, attribute: 'virtualizerprops' }),
69
+ __metadata("design:type", Object)
70
+ ], VirtualizedWrapper.prototype, "virtualizerprops", void 0);
71
+ __decorate([
72
+ state(),
73
+ __metadata("design:type", Object)
74
+ ], VirtualizedWrapper.prototype, "list", void 0);
75
+ __decorate([
76
+ state(),
77
+ __metadata("design:type", Object)
78
+ ], VirtualizedWrapper.prototype, "listItemTexts", void 0);
79
+ VirtualizedWrapper.register('mdc-virtualizedwrapper');
@@ -0,0 +1,2 @@
1
+ declare const styles: import("lit").CSSResult[];
2
+ export default styles;
@@ -0,0 +1,17 @@
1
+ import { css } from 'lit';
2
+ const styles = [
3
+ css `
4
+ :host {
5
+ .mdc-virtualizedlist-scroll {
6
+ height: 100%;
7
+ width: 100%;
8
+ overflow-y: auto;
9
+ }
10
+ .mdc-virtualizedlist-container {
11
+ position: relative;
12
+ width: 100%;
13
+ }
14
+ }
15
+ `,
16
+ ];
17
+ export default styles;
@@ -0,0 +1,17 @@
1
+ import { CSSResult, PropertyValues, TemplateResult } from 'lit';
2
+ import { VirtualizerProps } from './virtualizedlist.types';
3
+ import { Component } from '../../models';
4
+ declare class VirtualizedWrapper extends Component {
5
+ onscroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
6
+ virtualizerprops: VirtualizerProps;
7
+ list: TemplateResult<1>;
8
+ listItemTexts: string[];
9
+ constructor();
10
+ update(changedProperties: PropertyValues): void;
11
+ connectedCallback(): void;
12
+ private updateListItemTextArray;
13
+ private setListData;
14
+ render(): TemplateResult<1>;
15
+ static styles: Array<CSSResult>;
16
+ }
17
+ export default VirtualizedWrapper;
@@ -0,0 +1,79 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { html } from 'lit';
11
+ import { property, state } from 'lit/decorators.js';
12
+ import { ref } from 'lit/directives/ref.js';
13
+ import { styleMap } from 'lit/directives/style-map.js';
14
+ import { Component } from '../../models';
15
+ class VirtualizedWrapper extends Component {
16
+ constructor() {
17
+ super();
18
+ this.virtualizerprops = { count: 100, estimateSize: () => 100 };
19
+ this.list = html ``;
20
+ this.listItemTexts = new Array(this.virtualizerprops.count)
21
+ .fill(true)
22
+ .map((_, index) => `list item number ${index}`);
23
+ this.onscroll = null;
24
+ this.setListData = this.setListData.bind(this);
25
+ }
26
+ update(changedProperties) {
27
+ super.update(changedProperties);
28
+ if (changedProperties.get('virtualizerprops')) {
29
+ this.updateListItemTextArray();
30
+ }
31
+ }
32
+ connectedCallback() {
33
+ var _a;
34
+ super.connectedCallback();
35
+ if ((_a = this.virtualizerprops) === null || _a === void 0 ? void 0 : _a.count) {
36
+ this.updateListItemTextArray();
37
+ }
38
+ }
39
+ updateListItemTextArray() {
40
+ var _a;
41
+ this.listItemTexts = new Array((_a = this.virtualizerprops) === null || _a === void 0 ? void 0 : _a.count)
42
+ .fill(true)
43
+ .map((_, index) => `list item number ${index}`);
44
+ }
45
+ setListData({ virtualItems, measureElement, listStyle }) {
46
+ if (virtualItems) {
47
+ this.list = html `<ul style="margin: 0;${styleMap(listStyle)}">${virtualItems.map((virtualItem) => html `<li role="listitem" key=${virtualItem.key} data-index=${virtualItem.index} ref=${ref(measureElement)}>
48
+ ${this.listItemTexts[virtualItem.index]}</li>`)}</ul>`;
49
+ }
50
+ }
51
+ render() {
52
+ return html `
53
+ <div style="height: 500px; width: 500px;">
54
+ <mdc-virtualizedlist
55
+ .onscroll=${this.onscroll}
56
+ .virtualizerprops=${this.virtualizerprops}
57
+ .setlistdata=${this.setListData}
58
+ >${this.list}</mdc-virtualizedlist></div>
59
+ `;
60
+ }
61
+ }
62
+ VirtualizedWrapper.styles = Component.styles;
63
+ __decorate([
64
+ property({ type: Function, attribute: 'onscroll' }),
65
+ __metadata("design:type", Object)
66
+ ], VirtualizedWrapper.prototype, "onscroll", void 0);
67
+ __decorate([
68
+ property({ type: Object, attribute: 'virtualizerprops' }),
69
+ __metadata("design:type", Object)
70
+ ], VirtualizedWrapper.prototype, "virtualizerprops", void 0);
71
+ __decorate([
72
+ state(),
73
+ __metadata("design:type", Object)
74
+ ], VirtualizedWrapper.prototype, "list", void 0);
75
+ __decorate([
76
+ state(),
77
+ __metadata("design:type", Object)
78
+ ], VirtualizedWrapper.prototype, "listItemTexts", void 0);
79
+ export default VirtualizedWrapper;
@@ -0,0 +1,8 @@
1
+ import { VirtualItem, VirtualizerOptions } from '@tanstack/virtual-core';
2
+ import { StyleInfo } from 'lit/directives/style-map.js';
3
+ export interface SetListDataProps {
4
+ virtualItems: Array<VirtualItem>;
5
+ measureElement: (node: Element | null | undefined) => void;
6
+ listStyle: Readonly<StyleInfo>;
7
+ }
8
+ export type VirtualizerProps = Partial<VirtualizerOptions<Element, Element>>;