-
Notifications
You must be signed in to change notification settings - Fork 334
Add box component #804
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
Open
Hdanzo
wants to merge
2
commits into
geist-org:master
Choose a base branch
from
Hdanzo:add-box-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add box component #804
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import {mount} from 'enzyme' | ||
import {Box} from 'components' | ||
|
||
describe('Button', () => { | ||
it('should render correctly', () => { | ||
const wrapper = mount(<Box>Box</Box>) | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
|
||
it('should render as the provided element', async () => { | ||
const wrapper = mount(<Box as='a' href='https://geist.com'>Box</Box>) | ||
|
||
expect(wrapper.exists('a[href="https://geist.com"]')).toBe(true) | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
|
||
it('should render with provided styles', async () => { | ||
document.body.innerHTML = '<div id="root"></div>'; | ||
const wrapper = mount(<Box px='2rem' w='300px' h='100px' my='2rem'>Box</Box>, { | ||
attachTo: document.querySelector('#root') as HTMLDivElement | ||
}) | ||
|
||
expect(wrapper.find('div').getDOMNode()).toHaveStyle({ | ||
display: 'block', | ||
lineHeight: '100px', | ||
fontSize: 'calc(1 * 16px)', | ||
width: '300px', | ||
height: '100px', | ||
margin: '2rem 0px 2rem 0px', | ||
visibility: 'visible', | ||
padding: '0px 2rem 0px 2rem' | ||
}) | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
|
||
it('filter out scale related props', () => { | ||
const wrapper = mount(<Box px='2rem'>Box</Box>) | ||
|
||
expect(wrapper.exists('div')).toBe(true) | ||
expect(wrapper.getDOMNode().hasAttribute('px')).toBe(false) | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
|
||
it('should forward the provided ref', () => { | ||
const ref = React.createRef<HTMLDivElement>() | ||
const wrapper = mount(<Box ref={ref}>Box</Box>) | ||
expect(wrapper.find('div').getDOMNode()).toEqual(ref.current) | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React from 'react' | ||
import {DynamicScales, makeScaleHandler, ScaleProps} from "../use-scale"; | ||
import useClasses from "../use-classes"; | ||
import useTheme from "../use-theme"; | ||
|
||
type PropsOf<E extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>> = | ||
JSX.LibraryManagedAttributes<E, React.ComponentPropsWithRef<E>>; | ||
|
||
export interface BoxOwnProps<E extends React.ElementType = React.ElementType> { | ||
as?: E; | ||
} | ||
|
||
export type BoxProps<E extends React.ElementType> = BoxOwnProps<E> & | ||
Omit<PropsOf<E>, keyof BoxOwnProps> & ScaleProps; | ||
|
||
const defaultElement = 'div'; | ||
|
||
export type BoxComponent = <E extends React.ElementType = typeof defaultElement>( | ||
props: BoxProps<E>, | ||
) => React.ReactElement | null | ||
|
||
export const Box: BoxComponent = React.forwardRef( | ||
<E extends React.ElementType = typeof defaultElement> | ||
({as, children, className, ...restProps}: BoxProps<E>, ref: typeof restProps.ref | null) => { | ||
const Element = as || defaultElement; | ||
const {layout} = useTheme() | ||
const { | ||
paddingLeft, | ||
pl, | ||
paddingRight, | ||
pr, | ||
paddingTop, | ||
pt, | ||
paddingBottom, | ||
pb, | ||
marginTop, | ||
mt, | ||
marginRight, | ||
mr, | ||
marginBottom, | ||
mb, | ||
marginLeft, | ||
ml, | ||
px, | ||
py, | ||
mx, | ||
my, | ||
width, | ||
height, | ||
font, | ||
w, | ||
h, | ||
margin, | ||
padding, | ||
unit = layout.unit, | ||
scale = 1, | ||
...innerProps | ||
} = restProps | ||
|
||
const SCALES: DynamicScales = { | ||
pt: makeScaleHandler(paddingTop ?? pt ?? py ?? padding, scale, unit), | ||
pr: makeScaleHandler(paddingRight ?? pr ?? px ?? padding, scale, unit), | ||
pb: makeScaleHandler(paddingBottom ?? pb ?? py ?? padding, scale, unit), | ||
pl: makeScaleHandler(paddingLeft ?? pl ?? px ?? padding, scale, unit), | ||
px: makeScaleHandler(px ?? paddingLeft ?? paddingRight ?? pl ?? pr ?? padding, scale, unit), | ||
py: makeScaleHandler(py ?? paddingTop ?? paddingBottom ?? pt ?? pb ?? padding, scale, unit), | ||
mt: makeScaleHandler(marginTop ?? mt ?? my ?? margin, scale, unit), | ||
mr: makeScaleHandler(marginRight ?? mr ?? mx ?? margin, scale, unit), | ||
mb: makeScaleHandler(marginBottom ?? mb ?? my ?? margin, scale, unit), | ||
ml: makeScaleHandler(marginLeft ?? ml ?? mx ?? margin, scale, unit), | ||
mx: makeScaleHandler(mx ?? marginLeft ?? marginRight ?? ml ?? mr ?? margin, scale, unit), | ||
my: makeScaleHandler(my ?? marginTop ?? marginBottom ?? mt ?? mb ?? margin, scale, unit), | ||
width: makeScaleHandler(width ?? w, scale, unit), | ||
height: makeScaleHandler(height ?? h, scale, unit), | ||
font: makeScaleHandler(font, scale, unit), | ||
} | ||
|
||
return ( | ||
<Element | ||
className={useClasses('box', className)} | ||
ref={ref} | ||
{...innerProps} | ||
> | ||
{children} | ||
<style jsx>{` | ||
.box { | ||
line-height: ${SCALES.height(1)}; | ||
font-size: ${SCALES.font(1)}; | ||
width: ${SCALES.width(0, 'auto')}; | ||
height: ${SCALES.height(0, 'auto')}; | ||
padding: ${SCALES.pt(0)} ${SCALES.pr(0)} ${SCALES.pb(0)} ${SCALES.pl(0)}; | ||
margin: ${SCALES.mt(0)} ${SCALES.mr(0)} ${SCALES.mb(0)} ${SCALES.ml(0)}; | ||
} | ||
`}</style> | ||
</Element> | ||
); | ||
}, | ||
); | ||
|
||
// Box.displayName = 'GeistBox' | ||
|
||
export default Box |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Box from './box' | ||
|
||
export type { BoxProps } from './box' | ||
export default Box |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Box } from 'components' | ||
import { Layout, Playground, Attributes } from 'lib/components' | ||
|
||
export const meta = { | ||
title: 'Box', | ||
group: 'Layout', | ||
} | ||
|
||
## Box | ||
|
||
Polymorphic scalable component. | ||
|
||
<Playground | ||
scope={{ Box }} | ||
code={` | ||
<Box> | ||
Hello | ||
</Box> | ||
`} | ||
/> | ||
|
||
<Playground | ||
title="Polymorphic property" | ||
desc="Provide an element to render the Box as." | ||
scope={{ Box }} | ||
code={` | ||
<Box as='a' href='#'> | ||
I am an anchor tag | ||
</Box> | ||
`} | ||
/> | ||
|
||
<Playground | ||
title="Scalability" | ||
desc="Use props to scale the Box." | ||
scope={{ Box }} | ||
code={` | ||
<Box font={3} py='3rem' mx='auto' width='300px'> | ||
I am scalable | ||
</Box> | ||
`} | ||
/> | ||
|
||
<Attributes edit="/pages/en-us/components/page.mdx"> | ||
<Attributes.Title>Box.Props</Attributes.Title> | ||
|
||
| Attribute | Description | Type | Accepted values | Default | | ||
| --------------- | ---------------------------------- | --------------------------------- | --------------------------- | --------- | | ||
| **as** | render mode | `React.ElementType` | `'span', 'p', 'form', ...` | `div` | | ||
| ... | scale props | `ScaleProps` | `'width', 'px', 'font', ...`| - | | ||
| ... | native props | `HTMLAttributes` | `'id', 'className', ...` | - | | ||
|
||
</Attributes> | ||
|
||
export default ({ children }) => <Layout meta={meta}>{children}</Layout> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Forgot about this, I'll work on it