Skip to content

Commit d6a2d33

Browse files
committed
feat: ✨ Added root ref to return value, and changed how width/height are used
1 parent 6b0ecd3 commit d6a2d33

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/createVirtualizedList.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface VirtualizedListArgs<T, ScrollElement extends Element = Element,
1414
data: () => T[]
1515
determineKey?: KeyFunction<T>
1616
itemHeight?: number
17-
width?: number
18-
height?: number
17+
width?: number | string
18+
height?: number | string
1919
rootProps?: Record<string, any>
2020
containerProps?: Record<string, any>
2121
itemProps?: Record<string, any>
@@ -46,6 +46,7 @@ export interface ItemArgs<T> {
4646
* @property {Record<string, any>} container - Getter for the container element props.
4747
* @property {(itemCreator: (args: ItemArgs<T>) => any) => (virtualItem: VirtualItem, virtualItemIndex: () => number) => any} items - Function to create wrapper for list items.
4848
* @property {VirtualItem[]} item - Getter for the array of virtual items.
49+
* @property {Accessor<Element | null>} rootRef - Root ref getter
4950
*
5051
* @example
5152
* const MyList = () => {
@@ -100,10 +101,12 @@ export function createVirtualizedList<T extends Primitive | ObjectWithKey>(args:
100101

101102
const estimateSize = createMemo(() => args?.estimateSize || ((index: number) => args.itemHeight || 50))
102103

103-
const initialRect = () => ({
104-
width: args?.width ?? args.initialRect?.width ?? 600,
105-
height: args?.height ?? args.initialRect?.height ?? 400,
106-
})
104+
const initialRect = () => {
105+
return ({
106+
width: args.initialRect?.width ?? (typeof args?.width == 'number' ? args.width : undefined ) ?? 600,
107+
height: args.initialRect?.height ?? (typeof args?.height == 'number' ? args.height : undefined ) ?? 400,
108+
})
109+
}
107110

108111
const measureElement = createMemo(() => {
109112
if (isServer) return undefined
@@ -135,7 +138,9 @@ export function createVirtualizedList<T extends Primitive | ObjectWithKey>(args:
135138
paddingEnd: currentArgs?.paddingEnd ?? 10,
136139
scrollPaddingStart: currentArgs?.scrollPaddingStart ?? 0,
137140
scrollPaddingEnd: currentArgs?.scrollPaddingEnd ?? 0,
138-
initialRect: currentArgs?.initialRect ?? initialRect(),
141+
get initialRect() {
142+
return currentArgs?.initialRect ?? initialRect()
143+
},
139144
initialOffset: currentArgs?.initialOffset ?? 0,
140145
onChange: currentArgs.onChange,
141146
scrollToFn: currentArgs?.scrollToFn ?? ((offset, { behavior }) => {
@@ -204,8 +209,8 @@ export function createVirtualizedList<T extends Primitive | ObjectWithKey>(args:
204209
'overflow-y': horizontal() ? 'hidden' : 'auto',
205210
'overflow-x': horizontal() ? 'auto' : 'hidden',
206211
position: 'relative',
207-
height: args?.height ? `${args.height}px` : '400px',
208-
width: args?.width ? `${args.width}px` : '100%',
212+
height: (typeof args?.height == 'number') ? `${args.height}px` : (typeof args?.height == 'string') ? args.height : '100%',
213+
width: (typeof args?.width == 'number') ? `${args.width}px` : (typeof args?.width == 'string') ? args.width : '100%',
209214
}
210215
const horizontalAttr = horizontal() ? "" : undefined
211216

@@ -290,7 +295,8 @@ export function createVirtualizedList<T extends Primitive | ObjectWithKey>(args:
290295
items: itemWrapper,
291296
get item() {
292297
return virtualizer.getVirtualItems()
293-
}
298+
},
299+
rootRef: rootElement
294300
}
295301
}
296302

0 commit comments

Comments
 (0)