= {
3 | horizontal: {
4 | display: 'flex',
5 | flexDirection: 'row',
6 | },
7 | vertical: {
8 | display: 'flex',
9 | flexDirection: 'column',
10 | },
11 | flexWrap: {
12 | display: 'flex',
13 | flexWrap: 'wrap',
14 | },
15 | centerHorizontal: {
16 | justifyContent: 'center',
17 | alignItems: 'center',
18 | },
19 | wrap: {
20 | flexWrap: 'wrap',
21 | },
22 | noWrap: {
23 | flexWrap: 'nowrap',
24 | },
25 | centerHorizontalH: {
26 | justifyContent: 'center',
27 | },
28 | centerHorizontalV: {
29 | alignItems: 'center',
30 | },
31 | centerVertical: {
32 | alignItems: 'center',
33 | justifyContent: 'center',
34 | },
35 | centerVerticalH: {
36 | alignItems: 'center',
37 | },
38 | centerVerticalV: {
39 | justifyContent: 'center',
40 | },
41 | spaceAround: {
42 | justifyContent: 'space-around',
43 | },
44 | spaceBetween: {
45 | justifyContent: 'space-between',
46 | },
47 | justifyEnd: {
48 | justifyContent: 'flex-end',
49 | },
50 | justifyStart: {
51 | justifyContent: 'flex-start',
52 | },
53 | alignStart: {
54 | alignContent: 'flex-start',
55 | },
56 | stretchSelf: {
57 | alignSelf: 'stretch',
58 | },
59 | alignItemsStart: {
60 | alignItems: 'flex-start',
61 | },
62 | selfEnd: {
63 | alignSelf: 'flex-end',
64 | },
65 | };
66 |
67 | export default flex;
68 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
3 | "include": ["src", "types"],
4 | "compilerOptions": {
5 | "module": "esnext",
6 | "lib": ["dom", "esnext"],
7 | "importHelpers": true,
8 | // output .d.ts declaration files for consumers
9 | "declaration": true,
10 | // output .js.map sourcemap files for consumers
11 | "sourceMap": true,
12 | // match output dir to input dir. e.g. dist/index instead of dist/src/index
13 | "rootDir": "./src",
14 | // stricter type-checking for stronger correctness. Recommended by TS
15 | "strict": true,
16 | // linter checks for common issues
17 | "noImplicitReturns": true,
18 | "noFallthroughCasesInSwitch": true,
19 | // noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
20 | "noUnusedLocals": true,
21 | "noUnusedParameters": true,
22 | // use Node's module resolution algorithm, instead of the legacy TS one
23 | "moduleResolution": "node",
24 | // transpile JSX to React.createElement
25 | "jsx": "react",
26 | // interop between ESM and CJS modules. Recommended by TS
27 | "esModuleInterop": true,
28 | // significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
29 | "skipLibCheck": true,
30 | // error out if import and file system have a casing mismatch. Recommended by TS
31 | "forceConsistentCasingInFileNames": true,
32 | // `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
33 | "noEmit": true,
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/common.ts:
--------------------------------------------------------------------------------
1 | import { CommonProps, margin } from './types';
2 | import { allChildrenSelector, getOppositeDirection, px } from './utils';
3 | import flex from './flex';
4 | import { CSSObject } from '@emotion/styled';
5 |
6 | export const common = (
7 | direction: 'horizontal' | 'vertical' | 'left' | 'top',
8 | reverse?: boolean
9 | ) => (p: CommonProps): CSSObject => {
10 | direction = reverse === true ? getOppositeDirection(direction) : direction;
11 | return {
12 | ...(!!p.spaceBetween && flex.spaceBetween),
13 | ...(!!p.wrap && flex.wrap),
14 | ...(!!p.spaceAround && flex.spaceAround),
15 | ...(!!p.justifyEnd && flex.justifyEnd),
16 | ...(!!p.justifyStart && flex.justifyStart),
17 | ...(!!p.alignStart && flex.alignStart),
18 | ...(!!p.alignItemsStart && flex.alignItemsStart),
19 | ...(!!p.flex && { flex: p.flex }),
20 | ...(!!p.debug && { border: '1px solid purple' }),
21 |
22 | ...(!!p.spaceFirst && {
23 | '&:first-child': {
24 | [margin[direction]]: `${px(p.spaceFirst)} !important`,
25 | },
26 | }),
27 | ...(!!p.spacing
28 | ? {
29 | [allChildrenSelector]: {
30 | [margin[direction]]: `${px(p.spacing)} !important`,
31 | ...(p.spaceBottom
32 | ? {
33 | marginBottom: `${px(p.spacing)} !important`,
34 | }
35 | : {}),
36 | },
37 | '& > :last-child': {
38 | [margin[direction]]: `0 !important`,
39 | ...(p.spaceBottom ? { marginBottom: `0 !important` } : {}),
40 | },
41 | }
42 | : {}),
43 |
44 | ...(!!p.noShrink
45 | ? {
46 | flexShrink: 0,
47 | }
48 | : {}),
49 | ...(!!p.fullW && { width: '100%' }),
50 | ...(!!p.fullH && { height: '100%' }),
51 | ...p.styles,
52 | };
53 | };
54 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.1",
3 | "license": "MIT",
4 | "main": "dist/index.js",
5 | "typings": "dist/index.d.ts",
6 | "files": [
7 | "dist",
8 | "src"
9 | ],
10 | "repository": "https://github.com/sizzyapp/chakra-layout-components",
11 | "engines": {
12 | "node": ">=10"
13 | },
14 | "scripts": {
15 | "start": "tsdx watch",
16 | "build": "tsdx build",
17 | "test": "tsdx test --passWithNoTests",
18 | "lint": "tsdx lint",
19 | "prepare": "tsdx build",
20 | "size": "size-limit",
21 | "analyze": "size-limit --why"
22 | },
23 | "husky": {
24 | "hooks": {
25 | "pre-commit": "tsdx lint"
26 | }
27 | },
28 | "prettier": {
29 | "printWidth": 80,
30 | "semi": true,
31 | "singleQuote": true,
32 | "trailingComma": "es5"
33 | },
34 | "name": "chakra-layout-components",
35 | "author": "Kitze",
36 | "module": "dist/chakra-layout-components.esm.js",
37 | "size-limit": [
38 | {
39 | "path": "dist/chakra-layout-components.cjs.production.min.js",
40 | "limit": "10 KB"
41 | },
42 | {
43 | "path": "dist/chakra-layout-components.esm.js",
44 | "limit": "10 KB"
45 | }
46 | ],
47 | "devDependencies": {
48 | "@chakra-ui/react": "^1.3.4",
49 | "@emotion/styled": "^11.1.5",
50 | "@size-limit/preset-small-lib": "^4.10.1",
51 | "@types/react": "^17.0.3",
52 | "@types/react-dom": "^17.0.2",
53 | "framer-motion": "^4.0.0",
54 | "husky": "^5.1.3",
55 | "prettier": "^2.2.1",
56 | "react": "^17.0.1",
57 | "react-dom": "^17.0.1",
58 | "size-limit": "^4.10.1",
59 | "styled-mixins": "^0.1.10",
60 | "tsdx": "^0.14.1",
61 | "tslib": "^2.1.0",
62 | "typescript": "^4.2.3"
63 | },
64 | "peerDependencies": {
65 | "@chakra-ui/react": "^1.3.4",
66 | "@emotion/styled": "^11.1.5",
67 | "framer-motion": "^4.0.0",
68 | "react": ">=16",
69 | "styled-mixins": "^0.1.10"
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Welcome to chakra-layout-components 👋
2 |
3 | [](#contributors-)
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | > chakra-ui Layout Helper Components
18 |
19 | This project simplify uses `horizontal`, `grid`,`vertical` and `space` for [`Chakra UI`](https://chakra-ui.com/).
20 |
21 | ## Props
22 |
23 | These props are applicable to both Horizontal and Vertical components.
24 |
25 | | Prop | Type |
26 | | --------------- | :-----------: |
27 | | wrap | boolean |
28 | | spaceBetween | boolean |
29 | | spaceAround | boolean |
30 | | justifyEnd | boolean |
31 | | justifyStart | boolean |
32 | | flex | number |
33 | | spaceFirst | number |
34 | | spacing | number |
35 | | spaceBottom | boolean |
36 | | alignStart | boolean |
37 | | alignItemsStart | boolean |
38 | | center | boolean |
39 | | centerV | boolean |
40 | | centerH | boolean |
41 | | noShrink | boolean |
42 | | styles | CSSProperties |
43 | | invert | boolean |
44 | | reverse | boolean |
45 | | fullW | boolean |
46 | | fullH | boolean |
47 | | debug | boolean |
48 |
49 | In addition to this, the components also support Chakra UI's [style props](https://chakra-ui.com/docs/features/style-props).
50 |
51 | ## Example
52 |
53 |
54 |
55 | [](https://codesandbox.io/s/chakra-layout-components-demo-vrd18?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.js&theme=dark)
56 |
57 | ```js
58 | const App = () => {
59 | return (
60 |
61 |
62 | We
63 | are
64 | Vertical
65 |
66 |
67 |
68 | We
69 | are
70 | Horizontal
71 |
72 |
73 |
74 |
75 |
76 | I am centered vertically
77 |
78 |
79 |
80 | I am centered Horizontally
81 |
82 |
83 |
84 | I am just centered
85 |
86 |
87 |
88 | I will take full width
89 |
90 |
91 | );
92 | };
93 | ```
94 |
95 | Produces:
96 |
97 | 
98 |
99 | ## Install
100 |
101 | ```sh
102 | yarn install && yarn watch
103 | ```
104 |
105 | ## Usage
106 |
107 | ```sh
108 | cd examples && yarn start
109 | ```
110 |
111 | See it in browser at http://localhost:1234
112 |
113 | ## Run tests
114 |
115 | ```sh
116 | yarn run test
117 | ```
118 |
119 | ## 🤝 Contributing
120 |
121 | Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/kitze/layout-styled-components/issues).
122 |
123 | ## Show your support
124 |
125 | Give a ⭐️ if this project helped you!
126 |
127 | ## Contributors ✨
128 |
129 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
130 |
131 |
132 |
133 |
134 |
140 |
141 |
142 |
143 |
144 |
145 |
146 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
--------------------------------------------------------------------------------