-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for "grid-template" prop
- Loading branch information
1 parent
81eac7c
commit 1f3b32e
Showing
14 changed files
with
207 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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,44 @@ | ||
describe('Grid template', function() { | ||
before(() => { | ||
cy.loadStory(['recipes'], ['all', 'grid-template']) | ||
}) | ||
|
||
beforeEach(() => { | ||
cy.setBreakpoint('xs') | ||
}) | ||
|
||
it('areas do not intersect', () => { | ||
cy.get('#header').notIntersectWith('#content') | ||
cy.setBreakpoint('md').then(() => { | ||
cy.get('#header').notIntersectWith('#content') | ||
}) | ||
}) | ||
|
||
describe('row', () => { | ||
it('supporst exact row height', () => { | ||
cy.get('#header').should('have.css', 'height', '100px') | ||
cy.setBreakpoint('md').then(() => { | ||
cy.get('#header').should('have.css', 'height', '500px') | ||
cy.get('#content').should('have.css', 'height', '500px') | ||
}) | ||
}) | ||
|
||
it('supports dynamic row height', () => { | ||
cy.get('#content').should('have.css', 'height', '60px') | ||
}) | ||
}) | ||
|
||
describe('column', () => { | ||
it('supports exact column width', () => { | ||
cy.setBreakpoint('md').then(() => { | ||
cy.get('#header').should('have.css', 'width', '200px') | ||
}) | ||
}) | ||
|
||
it('supports dynamic column width', () => { | ||
cy.setBreakpoint('md').then(() => { | ||
cy.get('#content').should('have.css', 'width', '543px') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
describe('Recipes', function() { | ||
require('./iterative-areas.spec') | ||
require('./grid-template.spec') | ||
}) |
This file contains 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,35 @@ | ||
import React from 'react' | ||
import { Composition } from 'atomic-layout' | ||
import Square from '@stories/Square' | ||
|
||
const templateMobile = ` | ||
header 100px | ||
content 1fr | ||
` | ||
|
||
const templateTablet = ` | ||
header content 500px | ||
/ 200px 1fr | ||
` | ||
|
||
const List = () => ( | ||
<Composition | ||
id="composition" | ||
template={templateMobile} | ||
templateMd={templateTablet} | ||
gutter={10} | ||
> | ||
{({ Header, Content }) => ( | ||
<> | ||
<Header id="header"> | ||
<Square>Header</Square> | ||
</Header> | ||
<Content id="content"> | ||
<Square>Content</Square> | ||
</Content> | ||
</> | ||
)} | ||
</Composition> | ||
) | ||
|
||
export default List |
This file contains 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 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 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 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 @@ | ||
export { default } from './isAreaName' |
This file contains 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,20 @@ | ||
import isAreaName from './isAreaName' | ||
|
||
describe('isAreaName', () => { | ||
describe('returns true', () => { | ||
it('when given area name', () => { | ||
expect(isAreaName('footer')).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('returns false', () => { | ||
it('when given numeric value', () => { | ||
expect(isAreaName('100px')).toBe(false) | ||
expect(isAreaName('2fr')).toBe(false) | ||
}) | ||
|
||
it('when given dimensional keyword', () => { | ||
expect(isAreaName('auto')).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains 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,10 @@ | ||
const keywords = ['/', 'auto'] | ||
|
||
// Determines if the given string is a valid area name. | ||
// Takes into account on the row/column dimensions and | ||
// keywords in the "grid-template" definition. | ||
export default function isAreaName(areaName: string): boolean { | ||
const startsWithNumber = /^[0-9]/.test(areaName) | ||
const isKeyword = keywords.includes(areaName) | ||
return !startsWithNumber && !isKeyword | ||
} |
This file contains 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
36 changes: 29 additions & 7 deletions
36
src/utils/strings/sanitizeTemplateArea/sanitizeTemplateArea.ts
This file contains 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 |
---|---|---|
@@ -1,15 +1,37 @@ | ||
import compose from '@utils/functions/compose' | ||
import isAreaName from '../isAreaName' | ||
|
||
type SanitizeTemplateArea = (str: string) => string | ||
|
||
/** | ||
* Trims whitespace, removes duplicate single quotes and enforces | ||
* that area line is wrapped in single quotes. | ||
*/ | ||
// Joins a given template string fragments into a valid template string. | ||
// Takes into account proper single quote wrapping around the areas | ||
// and no single quotes around the dimensions (row/columns). | ||
const joinTemplateFragments = (fragments: string[]): string => { | ||
const areas = [] | ||
const suffixes = [] | ||
|
||
fragments.forEach((areaName) => { | ||
if (isAreaName(areaName)) { | ||
areas.push(areaName) | ||
} else { | ||
suffixes.push(areaName) | ||
} | ||
}) | ||
|
||
const joinedAreas = areas.length > 0 ? `'${areas.join(' ')}'` : '' | ||
const joinedSuffixes = suffixes.join(' ') | ||
|
||
return [joinedAreas, joinedSuffixes].filter(Boolean).join(' ') | ||
} | ||
|
||
// Prepares given "grid-template-areas" string to be digestible. | ||
// Trims whitespace, deduplicates single quotes and wraps | ||
// each line in single quotes. | ||
const sanitizeTemplateArea: SanitizeTemplateArea = compose( | ||
(area: string) => area.replace(/^.+$/gm, (areaName) => `'${areaName}'`), | ||
(area: string) => area.replace(/'+/gm, ''), | ||
(area: string) => area.trim(), | ||
joinTemplateFragments, | ||
(area: string): string[] => area.split(' '), | ||
(area: string): string => area.replace(/'+/gm, ''), | ||
(area: string): string => area.trim(), | ||
) | ||
|
||
export default sanitizeTemplateArea |
This file contains 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
28 changes: 15 additions & 13 deletions
28
src/utils/strings/sanitizeTemplateString/sanitizeTemplateString.ts
This file contains 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 |
---|---|---|
@@ -1,28 +1,30 @@ | ||
import compose from '@utils/functions/compose' | ||
import isAreaName from '../isAreaName' | ||
|
||
type SanitizeTemplateString = (str: string) => string[] | ||
|
||
/** | ||
* Returns an array of unique normalized grid areas | ||
* from the given template string. | ||
*/ | ||
// Returns an array of unique normalized grid areas | ||
// from the given template string. | ||
const sanitizeTemplateString: SanitizeTemplateString = compose( | ||
(list: string[]): string[] => list.sort(), | ||
|
||
/* Deduplicates area strings */ | ||
// Deduplicate area strings | ||
(list: string[]): string[] => Array.from(new Set(list)), | ||
|
||
/* Filters out empty area strings */ | ||
(arr: any[]) => arr.filter(Boolean), | ||
// Filter out "template" row/columns sizes | ||
(arr: string[]): string[] => arr.filter(isAreaName), | ||
|
||
/* Splits into a list of areas */ | ||
(str: string) => str.split(' '), | ||
// Filter out empty area strings | ||
(arr: string[]): string[] => arr.filter(Boolean), | ||
|
||
/* Deduplicates multiple spaces */ | ||
(str: string) => str.replace(/\s+/g, ' '), | ||
// Split into a list of areas | ||
(str: string): string[] => str.split(' '), | ||
|
||
/* Replaces newlines and single quotes with spaces */ | ||
(str: string) => str.replace(/\r?\n|\'+/g, ' '), | ||
// Deduplicate multiple spaces | ||
(str: string): string => str.replace(/\s+/g, ' '), | ||
|
||
// Replace new lines and single quotes with spaces | ||
(str: string): string => str.replace(/\r?\n|\'+/g, ' '), | ||
) | ||
|
||
export default sanitizeTemplateString |
This file contains 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