Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 75b9a9b

Browse files
authored
[WINDOW]: update gap property to not apply margins to the edge elements. (#155)
1 parent de3f432 commit 75b9a9b

File tree

12 files changed

+140
-137
lines changed

12 files changed

+140
-137
lines changed

.changeset/weak-trainers-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@resembli/react-virtualized-window": patch
3+
---
4+
5+
Change gap margins to no longer be applied to the first and last element.

book/.vitebook/theme/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import Theme from "@vitebook/theme-default"
22

3+
import "./main.css"
4+
35
export default Theme

book/.vitebook/theme/main.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.page.__vbk__ {
2+
height: 100%;
3+
}
4+
5+
.page__container.__vbk__ {
6+
width: 100%;
7+
height: 100%;
8+
}
9+
10+
.page__container.__vbk__ .__vbk__ {
11+
width: 95%;
12+
height: 95%;
13+
display: flex;
14+
justify-content: center;
15+
align-items: center;
16+
}

book/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
"preview": "vitebook preview"
1111
},
1212
"dependencies": {
13+
"@resembli/react-virtualized-window": "workspace:^",
14+
"@stitches/core": "^1.2.6",
1315
"react": "^17.0.2",
1416
"react-dom": "^17.0.2"
1517
},
1618
"devDependencies": {
1719
"@babel/core": "^7.15.8",
18-
"preact": "^10.5.14",
19-
"preact-render-to-string": "^5.1.19",
2020
"@preact/preset-vite": "^2.1.7",
2121
"@prefresh/vite": "^2.2.6",
2222
"@types/react": "^17.0.39",
2323
"@types/react-dom": "^17.0.10",
2424
"@vitebook/client": "^0.22.0",
25-
"@vitebook/markdown-preact": "^0.22.0",
2625
"@vitebook/core": "^0.22.0",
26+
"@vitebook/markdown-preact": "^0.22.0",
2727
"@vitebook/preact": "^0.22.0",
2828
"@vitebook/theme-default": "^0.22.0",
29+
"preact": "^10.5.14",
30+
"preact-render-to-string": "^5.1.19",
2931
"svelte": "^3.43.1",
3032
"typescript": "^4.4.0",
3133
"vite": "^2.7.0"

book/src/Button/Button.story.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.

book/src/Button/Button.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

book/src/Button/docs.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

book/src/Grid/Grid.story.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { css } from "@stitches/core"
2+
import type { PageMeta } from "@vitebook/client"
3+
import { Variant } from "@vitebook/preact"
4+
import { ControlsAddon } from "@vitebook/preact/addons"
5+
import { useState } from "react"
6+
7+
import { Grid } from "@resembli/react-virtualized-window"
8+
9+
export const __pageMeta: PageMeta = {
10+
title: "Grid",
11+
description: "Grid Playground",
12+
}
13+
14+
const data = Array.from({ length: 1000 }, (_, row) => {
15+
return Array.from({ length: 100 }, (_, column) => {
16+
return [row, column]
17+
})
18+
})
19+
20+
const itemClass = css({
21+
display: "flex",
22+
alignItems: "center",
23+
justifyContent: "center",
24+
backgroundColor: "black",
25+
variants: {
26+
odd: {
27+
true: {
28+
backgroundColor: "darkblue",
29+
},
30+
},
31+
},
32+
})
33+
34+
function GridPlayground() {
35+
const [gap, setGap] = useState(10)
36+
37+
return (
38+
<>
39+
<Variant name="Default" description="Default Grid">
40+
<Grid
41+
rtl
42+
data={data}
43+
defaultRowHeight={100}
44+
defaultColumnWidth={100}
45+
gap={gap}
46+
style={{ background: "red" }}
47+
>
48+
{({ data, style }) => (
49+
<div style={style} className={itemClass({ odd: (data[0] + data[1]) % 2 === 1 })}>
50+
{data[0]},{data[1]}
51+
</div>
52+
)}
53+
</Grid>
54+
</Variant>
55+
<Variant name="Half W/H" description="Half width and height">
56+
<Grid
57+
data={data}
58+
defaultRowHeight={50}
59+
defaultColumnWidth={50}
60+
width="50%"
61+
height="50%"
62+
gap={gap}
63+
>
64+
{({ data, style }) => (
65+
<div style={style} className={itemClass({ odd: (data[0] + data[1]) % 2 === 1 })}>
66+
{data[0]},{data[1]}
67+
</div>
68+
)}
69+
</Grid>
70+
</Variant>
71+
72+
<ControlsAddon>
73+
<label>
74+
Gap
75+
<input
76+
type="number"
77+
title="Gap"
78+
onChange={(e) => setGap(Number.parseInt(e.target.value))}
79+
value={gap}
80+
/>
81+
</label>
82+
</ControlsAddon>
83+
</>
84+
)
85+
}
86+
87+
GridPlayground.displayName = "GridPlayground"
88+
89+
export default GridPlayground

packages/react-virtualized-window/src/components/Grid.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import * as React from "react"
22

33
import { SizingDiv } from "../SizingDiv"
44
import { StickyDiv } from "../StickyDiv"
5-
import {
6-
getHorizontalGap,
7-
getHorizontalMarginStyling,
8-
getVerticalGap,
9-
getVerticalMarginStyling,
10-
} from "../itemGapUtilities"
5+
import { getHorizontalGap, getVerticalGap } from "../itemGapUtilities"
116
import type { NumberOrPercent, VirtualWindowBaseProps } from "../types"
127
import { useDataDimension } from "../useDataDimension"
138
import { useIndicesForDimensions } from "../useDimensionIndices"
@@ -127,7 +122,6 @@ export function Grid<T>({
127122

128123
const scrollableItems = React.useMemo(
129124
function Items() {
130-
const verticalMarginStyles = getVerticalMarginStyling(gap)
131125
return (
132126
<>
133127
<div style={{ height: runningHeight }}></div>
@@ -144,8 +138,8 @@ export function Grid<T>({
144138
key={cellKey}
145139
itemWidth={itemWidth}
146140
Component={children}
147-
rtl={rtl}
148-
itemGap={gap}
141+
marginLeft={rtl || j + horiStart === 0 ? 0 : horizontalGap}
142+
marginRight={!rtl || j + horiStart === 0 ? 0 : horizontalGap}
149143
itemProps={cell}
150144
column={horiStart + j}
151145
row={vertStart + i}
@@ -161,7 +155,8 @@ export function Grid<T>({
161155
height: itemHeight,
162156
minHeight: itemHeight,
163157
maxHeight: itemHeight,
164-
...verticalMarginStyles,
158+
marginTop: i + vertStart === 0 ? 0 : verticalGap,
159+
marginBottom: i + vertStart === data.length - 1 ? 0 : verticalGap,
165160
}}
166161
>
167162
<div style={{ width: runningWidth }} />
@@ -177,15 +172,16 @@ export function Grid<T>({
177172
data,
178173
dataHeights,
179174
dataWidths,
180-
gap,
181175
getKey,
182176
horiEnd,
183177
horiStart,
178+
horizontalGap,
184179
rtl,
185180
runningHeight,
186181
runningWidth,
187182
vertEnd,
188183
vertStart,
184+
verticalGap,
189185
],
190186
)
191187

@@ -241,33 +237,33 @@ export function Grid<T>({
241237

242238
type RenderItemsProps<T> = {
243239
Component: GridProps<T>["children"]
244-
itemGap: GridProps<T>["gap"]
245240
itemProps: T
246241
itemWidth: number
247242
column: number
248243
row: number
249-
rtl?: boolean
244+
marginLeft: number
245+
marginRight: number
250246
}
251247

252248
const RenderItem = function <T>({
253249
Component,
254-
itemGap,
255-
rtl,
256250
itemProps,
257251
itemWidth,
258252
column,
259253
row,
254+
marginRight,
255+
marginLeft,
260256
}: RenderItemsProps<T>) {
261257
const itemStyles = React.useMemo(() => {
262-
const marginStyling = getHorizontalMarginStyling(itemGap, rtl)
263258
return {
264259
width: itemWidth,
265260
minWidth: itemWidth,
266261
maxWidth: itemWidth,
267262
height: "100%",
268-
...marginStyling,
263+
marginLeft,
264+
marginRight,
269265
}
270-
}, [rtl, itemGap, itemWidth])
266+
}, [itemWidth, marginLeft, marginRight])
271267

272268
const cellMeta = React.useMemo<CellMeta>(() => ({ row, column }), [column, row])
273269

packages/react-virtualized-window/src/itemGapUtilities.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
11
import type { ItemGap } from "./types"
22

3-
export function getVerticalMarginStyling(itemGap?: ItemGap) {
4-
if (!itemGap) return { marginTop: 0, marginBottom: 0 }
5-
6-
const gap = getVerticalGap(itemGap)
7-
8-
return { marginTop: gap, marginBottom: gap }
9-
}
10-
11-
export function getHorizontalMarginStyling(itemGap?: ItemGap, rtl?: boolean) {
12-
if (!itemGap) return { marginLeft: 0, marginRight: 0 }
13-
14-
const gap = getHorizontalGap(itemGap)
15-
16-
return {
17-
marginLeft: rtl ? 0 : gap,
18-
marginRight: rtl ? gap : 0,
19-
}
20-
}
21-
223
export function getVerticalGap(itemGap?: ItemGap) {
234
return typeof itemGap === "number" ? itemGap : itemGap?.vertical ?? 0
245
}

0 commit comments

Comments
 (0)