Skip to content

Commit cc227bb

Browse files
committed
feat: add index css variable to column count
1 parent 78f2057 commit cc227bb

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

packages/components/grid/src/Column.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from "react";
1+
import React, { useEffect, useState } from "react";
22
import styles from "./styles/column.module.scss";
33
import classNames from "classnames";
44
import { useGridContext } from "./hooks";
@@ -16,19 +16,22 @@ export const GridColumn = ({
1616
style,
1717
...otherProps
1818
}: Readonly<GridColumnProps>): React.ReactElement => {
19+
const [index, setIndex] = useState<number | undefined>(undefined);
1920
const { columnsCount, registerColumn } = useGridContext();
2021

2122
useEffect(() => {
22-
const deregisterColumn = registerColumn();
23+
const { index, deregister } = registerColumn();
2324

24-
return deregisterColumn;
25+
setIndex(index);
26+
27+
return deregister;
2528
}, [registerColumn]);
2629

2730
return (
2831
<div
2932
style={{
3033
...style,
31-
...{ "--size": size, "--columns": columnsCount },
34+
...{ "--size": size, "--columns": columnsCount, "--index": index },
3235
}}
3336
className={classNames(
3437
styles.root,

packages/components/grid/src/context.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import React from "react";
22

33
export interface GridContextProps {
44
columnsCount: number;
5-
registerColumn: () => () => void;
5+
registerColumn: () => {
6+
deregister: () => void;
7+
index: number;
8+
};
69
}
710

811
export const GridContext = React.createContext<GridContextProps>({
912
columnsCount: 0,
1013
registerColumn() {
11-
return (): ReturnType<GridContextProps["registerColumn"]> => () => {
12-
throw new Error("Grid context not initialized");
13-
};
14+
throw new Error("Grid context not initialized");
1415
},
1516
});

packages/components/grid/src/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useMemo, useState } from "react";
1+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
22
import styles from "./styles/container.module.scss";
33
import classNames from "classnames";
44
import { GridContext, type GridContextProps } from "./context";
@@ -23,13 +23,18 @@ const Grid = ({
2323
className,
2424
...otherProps
2525
}: Readonly<GridProps>): React.ReactElement => {
26+
const index = useRef(0);
2627
const [columnsCount, setColumnsCount] = useState(0);
2728

2829
const registerColumn = useCallback<GridContextProps["registerColumn"]>(() => {
2930
setColumnsCount((v) => v + 1);
3031

31-
return () => {
32-
setColumnsCount((v) => v - 1);
32+
return {
33+
index: (index.current += 1),
34+
deregister: (): void => {
35+
index.current -= 1;
36+
setColumnsCount((v) => v - 1);
37+
},
3338
};
3439
}, []);
3540

0 commit comments

Comments
 (0)