-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(grid): add container component #128
Changes from all commits
41158f2
a8ff6ce
979d246
384a387
91762e3
26c533b
b28e13a
6569e1c
54fe39e
d514a59
f1bddf5
b727026
8419f30
df3fd9c
898bfcd
ae25439
3d85b4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { FC } from 'react'; | ||
|
||
import Col from './components/Col'; | ||
import { ColProps } from './components/Col.types'; | ||
import Container from './components/Container'; | ||
import Row from './components/Row'; | ||
import { RowProps } from './components/Row.types'; | ||
|
||
const Grid: { | ||
Row: FC<RowProps>; | ||
Col: FC<ColProps>; | ||
displayName: string; | ||
} = { | ||
const Grid = { | ||
Row, | ||
Col, | ||
Container, | ||
displayName: 'Grid', | ||
}; | ||
} as const; | ||
|
||
export default Grid; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import Container from '../components/Container'; | ||
|
||
describe('Grid.Container', () => { | ||
describe('Snapshots tests', () => { | ||
it('should match snapshot', () => { | ||
const { container } = render(<Container>content</Container>); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should set tag correctly', () => { | ||
const { getByText } = render( | ||
<Container component="section">hey</Container>, | ||
); | ||
|
||
expect(getByText('hey').tagName).toBe('SECTION'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Grid.Container Snapshots tests should match snapshot 1`] = ` | ||
<div> | ||
<div | ||
class="container" | ||
> | ||
content | ||
</div> | ||
</div> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@import '../../../themes/src/default.css'; | ||
|
||
.container { | ||
width: 100%; | ||
margin: 0 auto; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
box-sizing: border-box; | ||
padding-left: var(--gap-l); | ||
padding-right: var(--gap-l); | ||
|
||
@media (--tablet) { | ||
padding-right: var(--gap-xl); | ||
padding-left: var(--gap-xl); | ||
} | ||
|
||
@media (--tablet-l) { | ||
padding-right: var(--gap-3xl); | ||
padding-left: var(--gap-3xl); | ||
} | ||
|
||
@media (--desktop) { | ||
padding-right: var(--gap-3xl); | ||
padding-left: var(--gap-3xl); | ||
} | ||
|
||
@media (--desktop-m) { | ||
padding-right: var(--gap-3xl); | ||
padding-left: var(--gap-3xl); | ||
} | ||
|
||
@media (--desktop-l) { | ||
max-width: 1440px; | ||
padding-right: var(--gap-9xl); | ||
padding-left: var(--gap-9xl); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { ContainerProps } from './Container.types'; | ||
|
||
import styles from './Container.module.css'; | ||
|
||
function Container<E extends React.ElementType = 'div'>({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe C could represent
Another possible way to simplify a bit and transfer the typing logic to just one place, something like this could be done:
|
||
className, | ||
children, | ||
component, | ||
...rest | ||
}: ContainerProps<E>) { | ||
const Component = component ?? 'div'; | ||
|
||
return React.createElement( | ||
Component, | ||
{ | ||
className: cn(styles.container, className), | ||
...rest, | ||
}, | ||
children, | ||
); | ||
} | ||
|
||
export default Container; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
|
||
type ContainerBaseProps<E extends React.ElementType> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very very nice typings \o/ |
||
/** | ||
* The component used for the root node. Either a string to use a HTML element or a component | ||
* @default "div" | ||
*/ | ||
component?: E; | ||
}; | ||
|
||
export type ContainerProps<E extends React.ElementType> = | ||
ContainerBaseProps<E> & | ||
Omit<React.ComponentProps<E>, keyof ContainerBaseProps<E>>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,8 @@ import { RowProps } from './Row.types'; | |
import guttersStyles from '../styles/gutters.module.css'; | ||
import styles from './Row.module.css'; | ||
|
||
function Row({ | ||
Component = 'div', | ||
function Row<E extends React.ElementType = 'div'>({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing applies here, but like you've said on the PR we could have a proper PR to then unify this type of logic |
||
component, | ||
className, | ||
gutter = { | ||
mobile: 8, | ||
|
@@ -24,11 +24,14 @@ function Row({ | |
justify = 'between', | ||
children, | ||
dataTestId, | ||
}: RowProps) { | ||
}: RowProps<E>) { | ||
const Component = component ?? 'div'; | ||
|
||
const gridClassNames = useMemo( | ||
() => getGridClassNames({ gutter }, guttersStyles), | ||
[gutter], | ||
); | ||
|
||
const classNames = cn( | ||
guttersStyles.row, | ||
styles.component, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO
: I think we should adjust other polymorphic components as well in a separate PR with this logic/changeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Pagination will also use something similar