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
chore: Add StaticStates wrapper #377
Merged
NicholasBoll
merged 25 commits into
Workday:master
from
mannycarrera4:mc-update-canvas-provider
Dec 26, 2019
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
cb165ca
chore: Refactor canvasProvider and extract util function
mannycarrera4 acad35d
chore: Rename function
mannycarrera4 47e16e0
fix: Rename prop name
mannycarrera4 0ae64b3
fix: Remove unused import
mannycarrera4 e2eb14d
fix: Remove space added
mannycarrera4 76bac48
chore: Rebase and rename prop
mannycarrera4 f48e7ce
fix: Fix merging conflicts
mannycarrera4 d49dc80
fix: Rename prop for decorator
mannycarrera4 39a06f1
fix: Rename prop and add readme
mannycarrera4 b57196e
feat: Add wrapper for staticState utility function
NicholasBoll cf4e352
fix: Add tests for changeToStaticStates and refactor decorator
mannycarrera4 45b512b
fix: Remove storybook parameter for testing
mannycarrera4 7e311d8
fix: Remove test code for buttons
mannycarrera4 c845fde
fix: Sort state stories last
mannycarrera4 5c8e2db
fix: Rename function for converting pseudo to class
mannycarrera4 cd0d94e
fix: Remove console.log and add back in stories
mannycarrera4 7d5c375
fix: Change styles to interpolation
mannycarrera4 10285a0
test: Add test for &:state
mannycarrera4 63821d7
fix: Change name of interface
mannycarrera4 d904496
fix: Nicholas is dope
mannycarrera4 4e74004
Merge branch 'master' into mc-update-canvas-provider
mannycarrera4 4b2952b
chore: Removed unneeded type from styled.ts
NicholasBoll d71362a
feat: Move static state conversion into our theme
NicholasBoll 413e5b6
fix: Remove disable input provider prop
mannycarrera4 eb73ce4
Merge branch 'master' into mc-update-canvas-provider
NicholasBoll File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,11 @@ | ||
import * as React from 'react'; | ||
import {useTheme} from './theming/useTheme'; | ||
import CanvasProvider from './CanvasProvider'; | ||
import {CanvasTheme} from './theming'; | ||
|
||
export const StaticStates: React.FC = ({children}) => { | ||
const theme: CanvasTheme & {_staticStates?: boolean} = useTheme(); | ||
theme._staticStates = true; | ||
|
||
return <CanvasProvider theme={theme}>{children}</CanvasProvider>; | ||
}; |
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 |
---|---|---|
|
@@ -7,21 +7,40 @@ const noop = (styles: any) => styles; | |
// Pulled from https://github.com/emotion-js/emotion/blob/master/packages/styled-base/src/utils.js#L6 (not exported) | ||
type Interpolations = Array<any>; | ||
|
||
export const convertToStaticStates = (obj?: CSSObject): CSSObject | undefined => { | ||
if (!obj) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj).reduce((result, key) => { | ||
const newKey = key | ||
.replace(/^:/, '&:') // handle shorthand like ":focus" | ||
.replace(/,(\s+):/g, ',$1&:') // handle selectors like ":focus, :hover" | ||
.replace(/:(focus|hover|active)/g, '.$1') | ||
.replace(/\[data\-whatinput="?(mouse|touch|keyboard|pointer)"?]/g, '[data-whatinput="noop"]'); | ||
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. Disables the |
||
const value = | ||
typeof obj[key] === 'object' ? convertToStaticStates(obj[key] as CSSObject) : obj[key]; | ||
const newObj = {...result, [newKey]: value}; | ||
return newObj; | ||
}, {}); | ||
}; | ||
|
||
function styled<Props>(node: any) { | ||
return (...args: Interpolation<Props>[]) => { | ||
const newArgs: Interpolations = args.map( | ||
interpolation => (props: Props & {theme: CanvasTheme; direction: ContentDirection}) => { | ||
interpolation => (props: Props & {theme: CanvasTheme & {_staticStates?: boolean}}) => { | ||
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. Not adding |
||
props.theme = useTheme(props.theme); | ||
const direction = props.theme.direction; | ||
const maybeFlip = direction === ContentDirection.RTL ? rtlCSSJS : noop; | ||
const maybeConvert = props.theme._staticStates ? convertToStaticStates : noop; | ||
|
||
try { | ||
if (typeof interpolation === 'function') { | ||
return maybeFlip(interpolation(props) as CSSObject); | ||
return maybeFlip(maybeConvert(interpolation(props)) as CSSObject); | ||
} | ||
return maybeFlip(interpolation as CSSObject); | ||
return maybeFlip(maybeConvert(interpolation) as CSSObject); | ||
} catch (e) { | ||
return interpolation; | ||
return maybeConvert(interpolation); | ||
} | ||
} | ||
); | ||
|
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,134 @@ | ||
import {convertToStaticStates} from '../lib/theming/styled'; | ||
|
||
describe('changeToStaticStates', () => { | ||
it('should convert ":hover" to "&.hover"', () => { | ||
const input = {':hover': {display: 'none'}}; | ||
const expected = { | ||
'&.hover': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert ":active" to "&.active"', () => { | ||
const input = {':active': {display: 'none'}}; | ||
const expected = { | ||
'&.active': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert ":focus" to "&.focus"', () => { | ||
const input = {':focus': {display: 'none'}}; | ||
const expected = { | ||
'&.focus': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert "&:hover" to "&.hover"', () => { | ||
const input = {'&:hover': {display: 'none'}}; | ||
const expected = { | ||
'&.hover': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert "&:focus" to "&.focus"', () => { | ||
const input = {'&:focus': {display: 'none'}}; | ||
const expected = { | ||
'&.focus': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert "div:focus" to "div.focus"', () => { | ||
const input = {'div:focus': {display: 'none'}}; | ||
const expected = { | ||
'div.focus': { | ||
display: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert nested object', () => { | ||
const input = { | ||
div: { | ||
span: { | ||
':hover': { | ||
display: 'none', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const expected = { | ||
div: { | ||
span: { | ||
'&.hover': { | ||
display: 'none', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should convert multi-selectors', () => { | ||
const input = { | ||
[`:hover, :focus`]: { | ||
display: 'none', | ||
}, | ||
}; | ||
const expected = { | ||
[`&.hover, &.focus`]: { | ||
display: 'none', | ||
}, | ||
}; | ||
|
||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should not convert :not or :disabled', () => { | ||
const input = { | ||
'&:not(:disabled)': { | ||
display: 'none', | ||
}, | ||
}; | ||
const expected = { | ||
'&:not(:disabled)': { | ||
display: 'none', | ||
}, | ||
}; | ||
|
||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
|
||
it('should handle falsy objects', () => { | ||
expect(convertToStaticStates(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('should remove data-whatinput modifiers', () => { | ||
const input = { | ||
'[data-whatinput="mouse"], [data-whatinput=keyboard], [data-whatinput="pointer"], [data-whatinput="touch"]': { | ||
outline: 'none', | ||
}, | ||
}; | ||
const expected = { | ||
'[data-whatinput="noop"], [data-whatinput="noop"], [data-whatinput="noop"], [data-whatinput="noop"]': { | ||
outline: 'none', | ||
}, | ||
}; | ||
expect(convertToStaticStates(input)).toEqual(expected); | ||
}); | ||
}); |
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.
This change allows for multi-tier sorting instead of a global sorting. For example, "Welcome" is the category and "getting-started" is the item. The below will produce a string like:
"0welcome--agetting-started"
Since the "a" is now added before "getting-started" instead of before "welcome", it allows sorting to be local to other items. Lowercase letters should be used in multi-tier sorting so it flows naturally with other items without disrupting them. For example, numbers are always before lowercase letters in ASCII. So even if you add a '9', it will sort first rather than the desired last which is why
z
is used below