-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
[react] Create Hidden component #146
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0db02e1
finish Hidden
siriwatknp 2b817dc
add non prod check
siriwatknp 94e0894
test Hidden
siriwatknp 5c6662b
revert unnecessary change
siriwatknp e14ba9b
rename to outputJsPath
siriwatknp cf67116
Merge branch 'master' of https://github.com/mui/pigment-css into reac…
siriwatknp 15df69a
fix test
siriwatknp b550802
remove ext
siriwatknp cc02bd2
move test file next to the implementation
siriwatknp 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 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 Hidden from '@pigment-css/react/Hidden'; | ||
|
||
export default function HiddenDemo() { | ||
return ( | ||
<div> | ||
<Hidden smDown> | ||
<div>Hidden on small screens and down</div> | ||
</Hidden> | ||
<Hidden mdUp> | ||
<div>Hidden on medium screens and up</div> | ||
</Hidden> | ||
<Hidden only="sm"> | ||
<div>Hidden on sm</div> | ||
</Hidden> | ||
<Hidden only={['md', 'xl']}> | ||
<div>Hidden on md and xl</div> | ||
</Hidden> | ||
</div> | ||
); | ||
} |
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,18 @@ | ||
import { Breakpoint } from './base'; | ||
import { PolymorphicComponent } from './Box'; | ||
|
||
type HiddenUp = { | ||
[key in Breakpoint as `${key}Up`]?: boolean; | ||
}; | ||
type HiddenDown = { | ||
[key in Breakpoint as `${key}Down`]?: boolean; | ||
}; | ||
|
||
interface HiddenBaseProps extends HiddenUp, HiddenDown { | ||
className?: string; | ||
only?: Breakpoint | Breakpoint[]; | ||
} | ||
|
||
declare const Hidden: PolymorphicComponent<HiddenBaseProps>; | ||
|
||
export default Hidden; |
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,126 @@ | ||
/* eslint-disable react/jsx-filename-extension */ | ||
import * as React from 'react'; | ||
import clsx from 'clsx'; | ||
import PropTypes from 'prop-types'; | ||
import { generateAtomics } from './generateAtomics'; | ||
|
||
const hiddenAtomics = generateAtomics(({ theme }) => { | ||
const conditions = {}; | ||
|
||
for (let i = 0; i < theme.breakpoints.keys.length; i += 1) { | ||
const breakpoint = theme.breakpoints.keys[i]; | ||
conditions[`${theme.breakpoints.keys[i]}Only`] = theme.breakpoints.only(breakpoint); | ||
conditions[`${theme.breakpoints.keys[i]}Up`] = theme.breakpoints.up(breakpoint); | ||
conditions[`${theme.breakpoints.keys[i]}Down`] = theme.breakpoints.down(breakpoint); | ||
} | ||
|
||
return { | ||
conditions, | ||
properties: { | ||
display: ['none'], | ||
}, | ||
}; | ||
}); | ||
|
||
const Hidden = React.forwardRef(function Hidden( | ||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ className, component = 'div', style, ...props }, | ||
ref, | ||
) { | ||
const rest = {}; | ||
const breakpointProps = {}; | ||
Object.keys(props).forEach((key) => { | ||
if (key.endsWith('Up') || key.endsWith('Down')) { | ||
breakpointProps[key] = 'none'; | ||
} else if (key === 'only') { | ||
if (typeof props[key] === 'string') { | ||
breakpointProps[`${props[key]}Only`] = 'none'; | ||
} | ||
if (Array.isArray(props[key])) { | ||
props[key].forEach((val) => { | ||
breakpointProps[`${val}Only`] = 'none'; | ||
}); | ||
} | ||
} else { | ||
rest[key] = props[key]; | ||
} | ||
}); | ||
const stackClasses = hiddenAtomics({ display: breakpointProps }); | ||
const Component = component; | ||
return ( | ||
<Component | ||
ref={ref} | ||
className={clsx(stackClasses.className, className)} | ||
style={{ ...style, ...stackClasses.style }} | ||
{...rest} | ||
/> | ||
); | ||
}); | ||
|
||
Hidden.propTypes = { | ||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* The content of the component. | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
* @ignore | ||
*/ | ||
className: PropTypes.string, | ||
/** | ||
* The component used for the root node. | ||
* Either a string to use a HTML element or a component. | ||
*/ | ||
component: PropTypes.elementType, | ||
/** | ||
* If `true`, screens this size and down are hidden. | ||
*/ | ||
lgDown: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and up are hidden. | ||
*/ | ||
lgUp: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and down are hidden. | ||
*/ | ||
mdDown: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and up are hidden. | ||
*/ | ||
mdUp: PropTypes.bool, | ||
/** | ||
* Hide the given breakpoint(s). | ||
*/ | ||
only: PropTypes.oneOfType([ | ||
PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']), | ||
PropTypes.arrayOf(PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])), | ||
]), | ||
/** | ||
* If `true`, screens this size and down are hidden. | ||
*/ | ||
smDown: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and up are hidden. | ||
*/ | ||
smUp: PropTypes.bool, | ||
/** | ||
* @ignore | ||
*/ | ||
style: PropTypes.object, | ||
/** | ||
* If `true`, screens this size and down are hidden. | ||
*/ | ||
xlDown: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and up are hidden. | ||
*/ | ||
xlUp: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and down are hidden. | ||
*/ | ||
xsDown: PropTypes.bool, | ||
/** | ||
* If `true`, screens this size and up are hidden. | ||
*/ | ||
xsUp: PropTypes.bool, | ||
}; | ||
|
||
export default Hidden; |
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
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,53 @@ | ||
import { expect } from 'chai'; | ||
import { valueToLiteral } from '../src/utils/valueToLiteral'; | ||
|
||
describe('valueToLiteral', () => { | ||
it('should work with undefined as a value', () => { | ||
expect( | ||
valueToLiteral({ | ||
foo: undefined, | ||
}), | ||
).to.deep.equal({ | ||
type: 'ObjectExpression', | ||
properties: [ | ||
{ | ||
type: 'ObjectProperty', | ||
computed: false, | ||
shorthand: false, | ||
key: { | ||
type: 'Identifier', | ||
name: 'foo', | ||
}, | ||
value: { | ||
type: 'Identifier', | ||
name: 'undefined', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('should work with null as a value', () => { | ||
expect( | ||
valueToLiteral({ | ||
foo: null, | ||
}), | ||
).to.deep.equal({ | ||
type: 'ObjectExpression', | ||
properties: [ | ||
{ | ||
type: 'ObjectProperty', | ||
computed: false, | ||
shorthand: false, | ||
key: { | ||
type: 'Identifier', | ||
name: 'foo', | ||
}, | ||
value: { | ||
type: 'NullLiteral', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Gist of the logic.