diff --git a/packages/atomic-layout-core/src/const/props.ts b/packages/atomic-layout-core/src/const/props.ts index f59f5262..db2358b9 100644 --- a/packages/atomic-layout-core/src/const/props.ts +++ b/packages/atomic-layout-core/src/const/props.ts @@ -1,4 +1,5 @@ import { Numeric } from './defaultOptions' +import { AreasMap } from '../utils/templates/generateComponents' type CSSGlobalValues = 'inherit' | 'initial' | 'unset' @@ -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 +} diff --git a/packages/atomic-layout-core/src/index.ts b/packages/atomic-layout-core/src/index.ts index 16a58548..ce31bc7e 100644 --- a/packages/atomic-layout-core/src/index.ts +++ b/packages/atomic-layout-core/src/index.ts @@ -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' diff --git a/packages/atomic-layout-emotion/LICENSE b/packages/atomic-layout-emotion/LICENSE new file mode 100644 index 00000000..6bb0b2bc --- /dev/null +++ b/packages/atomic-layout-emotion/LICENSE @@ -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. \ No newline at end of file diff --git a/packages/atomic-layout-emotion/package.json b/packages/atomic-layout-emotion/package.json new file mode 100644 index 00000000..8458a4b6 --- /dev/null +++ b/packages/atomic-layout-emotion/package.json @@ -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": "kettanaito@gmail.com" + }, + "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" + ] +} diff --git a/packages/atomic-layout-emotion/src/components/Box.tsx b/packages/atomic-layout-emotion/src/components/Box.tsx new file mode 100644 index 00000000..2af39e6f --- /dev/null +++ b/packages/atomic-layout-emotion/src/components/Box.tsx @@ -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 = styled.div` + display: ${({ flex, inline }) => + flex + ? inline + ? 'inline-flex' + : 'flex' + : inline + ? 'inline-block' + : 'block'}; + + && { + ${applyStyles} + } +` + +export default Box diff --git a/packages/atomic-layout-emotion/src/components/Composition.tsx b/packages/atomic-layout-emotion/src/components/Composition.tsx new file mode 100644 index 00000000..382f0799 --- /dev/null +++ b/packages/atomic-layout-emotion/src/components/Composition.tsx @@ -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` + && { + ${applyStyles}; + display: ${({ inline }) => (inline ? 'inline-grid' : 'grid')}; + } +` + +const createAreaComponent = (areaName: string): AreaComponent => ( + props: BoxProps, +) => + +const Composition: React.FC = ({ + 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 ( + + {hasAreaComponents && hasRenderProp + ? (children as CompositionRenderProp)(Areas) + : children} + + ) +} + +export default Composition diff --git a/packages/atomic-layout-emotion/src/index.ts b/packages/atomic-layout-emotion/src/index.ts new file mode 100644 index 00000000..96732dd8 --- /dev/null +++ b/packages/atomic-layout-emotion/src/index.ts @@ -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' diff --git a/packages/atomic-layout-emotion/tslint.json b/packages/atomic-layout-emotion/tslint.json new file mode 100644 index 00000000..77144b79 --- /dev/null +++ b/packages/atomic-layout-emotion/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": ["../atomic-layout/tslint.json"] +} diff --git a/packages/atomic-layout/src/components/Composition.tsx b/packages/atomic-layout/src/components/Composition.tsx index 51d8f8f0..af88c697 100644 --- a/packages/atomic-layout/src/components/Composition.tsx +++ b/packages/atomic-layout/src/components/Composition.tsx @@ -1,10 +1,9 @@ import * as React from 'react' import styled from 'styled-components' import { - GenericProps, BoxProps, - GridProps, - AreasMap, + CompositionProps, + CompositionRenderProp, AreaComponent, parseTemplates, generateComponents, @@ -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` && { ${applyStyles}; @@ -60,7 +51,7 @@ const Composition: React.FC = ({ return ( {hasAreaComponents && hasChildrenFunction - ? (children as ChildrenFunction)(Areas) + ? (children as CompositionRenderProp)(Areas) : children} )