Skip to content

Commit

Permalink
Adds basics of the emotion support
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Dec 7, 2019
1 parent 0a90e75 commit 999aa66
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 13 deletions.
9 changes: 9 additions & 0 deletions packages/atomic-layout-core/src/const/props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Numeric } from './defaultOptions'
import { AreasMap } from '../utils/templates/generateComponents'

type CSSGlobalValues = 'inherit' | 'initial' | 'unset'

Expand Down Expand Up @@ -344,3 +345,11 @@ export interface BoxProps extends GenericProps {
flex?: boolean
inline?: boolean
}

export type CompositionRenderProp = (areas: AreasMap) => React.ReactNode

export interface CompositionProps extends GenericProps, GridProps {
[propName: string]: any
children: CompositionRenderProp | React.ReactNode
inline?: boolean
}
8 changes: 7 additions & 1 deletion packages/atomic-layout-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export {
Breakpoints,
BreakpointBehavior,
} from './const/defaultOptions'
export { GenericProps, BoxProps, GridProps } from './const/props'
export {
GenericProps,
GridProps,
BoxProps,
CompositionProps,
CompositionRenderProp,
} from './const/props'

/* Styles */
export { default as applyStyles } from './utils/styles/applyStyles'
Expand Down
9 changes: 9 additions & 0 deletions packages/atomic-layout-emotion/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2018 Artem Zakharchenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 changes: 45 additions & 0 deletions packages/atomic-layout-emotion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@atomic-layout/emotion",
"description": "Physical representation of layout composition to create declarative, responsive layouts in React.",
"version": "0.11.0",
"license": "MIT",
"scripts": {
"lint": "tslint -c tslint.json 'src/**/*.{ts,tsx}'"
},
"peerDependencies": {
"react": ">= 16.8",
"@emotion/core": ">= 10.0",
"@emotion/styled": ">= 10.0"
},
"dependencies": {
"@atomic-layout/core": "^0.11.0"
},
"files": [
"lib",
"README.md"
],
"author": {
"name": "Artem Zakharchenko",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/kettanaito/atomic-layout/packages/atomic-layout-emotion"
},
"bugs": {
"url": "https://github.com/kettanaito/atomic-layout/issues"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/atomic-layout"
},
"keywords": [
"atomic",
"layout",
"responsive",
"composition",
"css grid",
"react",
"emotion"
]
}
20 changes: 20 additions & 0 deletions packages/atomic-layout-emotion/src/components/Box.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react'
import styled from '@emotion/styled'
import { BoxProps, applyStyles } from '@atomic-layout/core'

const Box: React.FC<BoxProps> = styled.div`
display: ${({ flex, inline }) =>
flex
? inline
? 'inline-flex'
: 'flex'
: inline
? 'inline-block'
: 'block'};
&& {
${applyStyles}
}
`

export default Box
49 changes: 49 additions & 0 deletions packages/atomic-layout-emotion/src/components/Composition.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react'
import {
AreaComponent,
BoxProps,
CompositionProps,
CompositionRenderProp,
applyStyles,
parseTemplates,
generateComponents,
} from '@atomic-layout/core'
import styled from '@emotion/styled'
import Box from './Box'
import { withPlaceholder } from '../../../atomic-layout/src/utils/withPlaceholder'

const CompositionWrapper = styled.div<CompositionProps>`
&& {
${applyStyles};
display: ${({ inline }) => (inline ? 'inline-grid' : 'grid')};
}
`

const createAreaComponent = (areaName: string): AreaComponent => (
props: BoxProps,
) => <Box area={areaName} {...props} />

const Composition: React.FC<CompositionProps> = ({
children,
...restProps
}) => {
const areasList = parseTemplates(restProps)
const Areas = generateComponents(
areasList,
createAreaComponent,
withPlaceholder,
)
const hasAreaComponents = Object.keys(Areas).length > 0
const childrenType = typeof children
const hasRenderProp = childrenType === 'function'

return (
<CompositionWrapper {...restProps}>
{hasAreaComponents && hasRenderProp
? (children as CompositionRenderProp)(Areas)
: children}
</CompositionWrapper>
)
}

export default Composition
16 changes: 16 additions & 0 deletions packages/atomic-layout-emotion/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Layout } from '@atomic-layout/core'
export default Layout

/* Components */
export { default as Box } from './components/Box'
export { default as Composition } from './components/Composition'

/* Hooks */
export {
MediaQuery,
useBreakpointChange,
useResponsiveComponent,
useResponsiveProps,
useResponsiveValue,
useViewportChange,
} from '../../atomic-layout/src'
3 changes: 3 additions & 0 deletions packages/atomic-layout-emotion/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../atomic-layout/tslint.json"]
}
15 changes: 3 additions & 12 deletions packages/atomic-layout/src/components/Composition.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as React from 'react'
import styled from 'styled-components'
import {
GenericProps,
BoxProps,
GridProps,
AreasMap,
CompositionProps,
CompositionRenderProp,
AreaComponent,
parseTemplates,
generateComponents,
Expand All @@ -14,14 +13,6 @@ import {
import Box from './Box'
import { withPlaceholder } from '../utils/withPlaceholder'

type ChildrenFunction = (areas: AreasMap) => React.ReactNode

interface CompositionProps extends GenericProps, GridProps {
[propName: string]: any
children: ChildrenFunction | React.ReactNode
inline?: boolean
}

const CompositionWrapper = styled.div<CompositionProps>`
&& {
${applyStyles};
Expand Down Expand Up @@ -60,7 +51,7 @@ const Composition: React.FC<CompositionProps> = ({
return (
<CompositionWrapper {...restProps}>
{hasAreaComponents && hasChildrenFunction
? (children as ChildrenFunction)(Areas)
? (children as CompositionRenderProp)(Areas)
: children}
</CompositionWrapper>
)
Expand Down

0 comments on commit 999aa66

Please sign in to comment.