Skip to content

Commit

Permalink
feat: add grid component
Browse files Browse the repository at this point in the history
  • Loading branch information
abelflopes committed Nov 9, 2023
1 parent 4ea4130 commit a27d7ac
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 1 deletion.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/components/grid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# RCK | React Grid Component

The React Grid component features a robust 12-column system, providing developers with a flexible framework to create responsive and well-structured layouts. This system allows elements to be precisely positioned within the grid, offering granular control over their placement and alignment. With the ability to utilize these 12 columns, developers can design layouts that are visually balanced and optimized for different screen sizes.

Moreover, the React Grid component offers the convenience of both wrapping and nowrap behaviors. When set to wrap, the grid intelligently adjusts its content, allowing items to flow into new rows as needed. This ensures that the layout gracefully accommodates varying amounts of content without sacrificing its structure. Conversely, the nowrap option prevents items from wrapping onto new lines, maintaining a compact and streamlined appearance.

By combining the 12-column system with the flexibility to wrap or nowrap elements, developers can effortlessly create responsive designs that adapt to diverse devices and content lengths. This versatile component empowers developers to craft visually appealing interfaces that maintain consistency and readability across different screen sizes, enhancing the overall user experience.

### Installation

To integrate the this component into your React apps, you can install it using npm or yarn: `npm i --save @react-ck/grid` or `yarn add @react-ck/grid`.
3 changes: 3 additions & 0 deletions packages/components/grid/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "@react-ck/babel-config",
};
4 changes: 4 additions & 0 deletions packages/components/grid/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.module.scss" {
const content: Record<string, string>;
export default content;
}
2 changes: 2 additions & 0 deletions packages/components/grid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Hack for module resolution of non built packages
export * from "./src/index";
1 change: 1 addition & 0 deletions packages/components/grid/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { config as default } from "@react-ck/jest-config";
38 changes: 38 additions & 0 deletions packages/components/grid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@react-ck/grid",
"private": false,
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"homepage": "https://github.com/abelflopes/react-ck/tree/master/packages/components/banner#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/abelflopes/react-ck.git"
},
"scripts": {
"build": "NODE_ENV=production webpack",
"lint:typescript": "tsc --noEmit",
"test": "npx -y npm-run-all -s test:*",
"test:unit": "jest --testPathPattern=\".unit.*\"",
"test:snapshot": "jest --testPathPattern=\".snapshot.*\"",
"test:snapshot:update": "jest --testPathPattern=\".snapshot.*\" -u"
},
"devDependencies": {
"@react-ck/babel-config": "^1.0.0",
"@react-ck/jest-config": "^1.0.0",
"@react-ck/typescript-config": "^1.0.0",
"@react-ck/webpack-config": "^1.0.0",
"@types/react": "^18.2.33"
},
"peerDependencies": {
"react": "^18.2.0"
},
"dependencies": {
"@react-ck/scss-utils": "^1.0.0",
"@react-ck/theme": "^1.0.0",
"classnames": "^2.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Snapshot GridColumn renders correctly 1`] = `
<div
className="root size_auto"
style={
{
"--columns": 0,
"--size": "auto",
}
}
/>
`;

exports[`Snapshot GridContainer renders correctly 1`] = `
<div
className="root wrap"
/>
`;
17 changes: 17 additions & 0 deletions packages/components/grid/specs/index.snapshot.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import { GridContainer, GridColumn } from "../src/index";
import renderer from "react-test-renderer";

describe("Snapshot GridContainer", () => {
test("renders correctly", async () => {
const tree = renderer.create(<GridContainer />).toJSON();
expect(tree).toMatchSnapshot();
});
});

describe("Snapshot GridColumn", () => {
test("renders correctly", async () => {
const tree = renderer.create(<GridColumn />).toJSON();
expect(tree).toMatchSnapshot();
});
});
20 changes: 20 additions & 0 deletions packages/components/grid/specs/index.unit.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import { GridContainer, GridColumn } from "../src/index";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";

describe("Unit GridContainer", () => {
test("renders correctly", async () => {
render(<GridContainer data-testid="container" />);
const find = await screen.findByTestId("container");
expect(find).toBeInTheDocument();
});
});

describe("Unit GridColumn", () => {
test("renders correctly", async () => {
render(<GridColumn data-testid="column" />);
const find = await screen.findByTestId("column");
expect(find).toBeInTheDocument();
});
});
41 changes: 41 additions & 0 deletions packages/components/grid/src/Column.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useEffect } from "react";
import styles from "./styles/column.module.scss";
import classNames from "classnames";
import { useGridContext } from "./context";

export interface GridColumnProps extends React.HTMLAttributes<HTMLHRElement> {
size?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | "auto";
}

export const GridColumn = ({
size = "auto",
className,
style,
...otherProps
}: Readonly<GridColumnProps>): React.ReactElement => {
const { columnsCount, registerColumn } = useGridContext();

useEffect(() => {
const deregisterColumn = registerColumn();

return deregisterColumn;
}, [registerColumn]);

return (
<div
style={{
...style,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
...({ "--size": size, "--columns": columnsCount } as React.CSSProperties),
}}
className={classNames(
styles.root,
{
[`${styles.size_auto}`]: size === "auto",
},
className,
)}
{...otherProps}
/>
);
};
47 changes: 47 additions & 0 deletions packages/components/grid/src/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useCallback, useMemo, useState } from "react";
import styles from "./styles/container.module.scss";
import classNames from "classnames";
import { GridContext, type GridContextProps } from "./context";

export interface GridContainerProps extends React.HTMLAttributes<HTMLHRElement> {
wrap?: boolean;
}

export const GridContainer = ({
wrap = true,
className,
...otherProps
}: Readonly<GridContainerProps>): React.ReactElement => {
const [columnsCount, setColumnsCount] = useState(0);

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

return () => {
setColumnsCount((v) => v - 1);
};
}, []);

const contextValue = useMemo<GridContextProps>(
() => ({
columnsCount,
registerColumn,
}),
[columnsCount, registerColumn],
);

return (
<GridContext.Provider value={contextValue}>
<div
className={classNames(
styles.root,
{
[`${styles.wrap}`]: wrap,
},
className,
)}
{...otherProps}
/>
</GridContext.Provider>
);
};
18 changes: 18 additions & 0 deletions packages/components/grid/src/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { useContext } from "react";

export interface GridContextProps {
columnsCount: number;
registerColumn: () => () => void;
}

export const themeContextDefaults: GridContextProps = {
columnsCount: 0,
registerColumn() {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
},
};

export const GridContext = React.createContext<GridContextProps>(themeContextDefaults);

export const useGridContext = (): GridContextProps => useContext(GridContext);
3 changes: 3 additions & 0 deletions packages/components/grid/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./Container";

export * from "./Column";
16 changes: 16 additions & 0 deletions packages/components/grid/src/styles/column.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import "@react-ck/theme";
@import "@react-ck/scss-utils";

.root {
width: calc(
(100% / 12 * var(--size)) -
(#{get-css-var(grid-container, gap)} * (var(--columns) - 1) / var(--columns))
);
flex-shrink: 0;
flex-grow: 0;
}

.size_auto {
flex-shrink: 1;
flex-grow: 1;
}
15 changes: 15 additions & 0 deletions packages/components/grid/src/styles/container.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import "@react-ck/theme";
@import "@react-ck/scss-utils";

.root {
@include define-css-var(divider, flex-wrap, none);
@include define-css-var(grid-container, gap, get-spacing(2));

display: flex;
gap: get-css-var(grid-container, gap);
flex-wrap: get-css-var(grid-container, flex-wrap);
}

.wrap {
@include define-css-var(grid-container, flex-wrap, wrap);
}
7 changes: 7 additions & 0 deletions packages/components/grid/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@react-ck/typescript-config/tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./*.d.ts", "./src/**/*", "./src/index.ts*"]
}
4 changes: 4 additions & 0 deletions packages/components/grid/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@react-ck/typescript-config/tsconfig.json",
"include": ["./**/*"]
}
6 changes: 6 additions & 0 deletions packages/components/grid/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getWebpackConfig } from "@react-ck/webpack-config";
import packageJson from "./package.json";

export default getWebpackConfig({
cssHashSalt: packageJson.name,
});
4 changes: 3 additions & 1 deletion packages/components/text/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Text
# RCK | React Text Component

The Text component is a highly adaptable tool designed for displaying textual content in diverse ways within a user interface. With its flexibility and versatility, developers can seamlessly integrate the Text component into their applications, leveraging its multiple variations and component polymorphism. By offering a range of visual styles and the ability to serve as a container for various text elements, this component enhances the overall design and user experience without delving into implementation specifics.

### Installation

Expand Down
1 change: 1 addition & 0 deletions packages/docs/stories/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@react-ck/container": "^1.0.0",
"@react-ck/data-table": "^1.0.0",
"@react-ck/divider": "^1.0.0",
"@react-ck/grid": "^1.0.0",
"@react-ck/icon": "^1.0.0",
"@react-ck/input": "^1.0.0",
"@react-ck/list": "^1.0.0",
Expand Down
Loading

0 comments on commit a27d7ac

Please sign in to comment.