-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[system] Add styled primitives #27207
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
608c4f8
wip
siriwatknp 18a73e6
attach primitives to styled-engine
siriwatknp 78af401
add styled primitive
siriwatknp 579e1ca
remove dev page
siriwatknp c308226
fix types build error
siriwatknp 9ea1f11
export Mui type
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
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
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,3 @@ | ||
export * from './primitives'; | ||
|
||
export { default as mui } from './primitives'; |
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,54 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { mui } from '@material-ui/core/primitives'; | ||
import { createTheme, hexToRgb, ThemeProvider } from '@material-ui/core/styles'; | ||
import { createClientRender } from 'test/utils'; | ||
|
||
describe('StyledPrimitive', () => { | ||
const theme = createTheme(); | ||
const render = createClientRender(); | ||
|
||
it('accepts sx prop', () => { | ||
const { container } = render(<mui.a sx={{ color: 'primary.main', pt: 2 }}>Foo</mui.a>); | ||
expect(container.firstChild).toHaveComputedStyle({ | ||
color: hexToRgb(theme.palette.primary.main), | ||
paddingTop: theme.spacing(2), | ||
}); | ||
}); | ||
|
||
it('extend prop', () => { | ||
const { container } = render( | ||
<mui.a color="primary.main" pt={2}> | ||
Foo | ||
</mui.a>, | ||
); | ||
expect(container.firstChild).toHaveComputedStyle({ | ||
color: hexToRgb(theme.palette.primary.main), | ||
paddingTop: theme.spacing(2), | ||
}); | ||
}); | ||
|
||
it('only html prop can spread to DOM', () => { | ||
const { container } = render( | ||
<mui.a alignItems="center" pt={2} href="/home" data-tag="a"> | ||
Foo | ||
</mui.a>, | ||
); | ||
expect(container.firstChild).to.have.attribute('href', '/home'); | ||
expect(container.firstChild).to.have.attribute('data-tag', 'a'); | ||
expect(container.firstChild).not.to.have.attribute('alignItems', 'center'); | ||
}); | ||
|
||
it('use value from custom theme', () => { | ||
const { container } = render( | ||
<ThemeProvider theme={createTheme({ palette: { primary: { main: '#ff5252' } } })}> | ||
<mui.a color="primary.main" pt={2}> | ||
Foo | ||
</mui.a> | ||
</ThemeProvider>, | ||
); | ||
expect(container.firstChild).toHaveComputedStyle({ | ||
color: hexToRgb('#ff5252'), | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { createPrimitiveStyled, StyledPrimitive } from '@material-ui/system'; | ||
import createTheme, { Theme } from '../styles/createTheme'; | ||
|
||
export type Mui = { | ||
[Key in keyof JSX.IntrinsicElements]: StyledPrimitive<Key, Theme>; | ||
}; | ||
|
||
const generatePrimitives = () => { | ||
const styled = createPrimitiveStyled(createTheme()); | ||
const mui = {}; | ||
|
||
styled.htmlTags.forEach((tag) => { | ||
// @ts-ignore | ||
mui[tag] = styled(tag)(); | ||
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. I am wondering what is the bundle size of this when we import it |
||
}); | ||
|
||
return mui as Mui; | ||
}; | ||
|
||
const mui = generatePrimitives(); | ||
|
||
export default mui; |
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 |
---|---|---|
|
@@ -25,35 +25,39 @@ declare global { | |
* @example expect(element).toHaveInlineStyle({ width: '200px' }) | ||
*/ | ||
toHaveInlineStyle( | ||
expectedStyle: Record< | ||
Exclude< | ||
keyof CSSStyleDeclaration, | ||
| 'getPropertyPriority' | ||
| 'getPropertyValue' | ||
| 'item' | ||
| 'removeProperty' | ||
| 'setProperty' | ||
| number | ||
>, | ||
string | ||
expectedStyle: Partial< | ||
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. It should have |
||
Record< | ||
Exclude< | ||
keyof CSSStyleDeclaration, | ||
| 'getPropertyPriority' | ||
| 'getPropertyValue' | ||
| 'item' | ||
| 'removeProperty' | ||
| 'setProperty' | ||
| number | ||
>, | ||
string | ||
> | ||
>, | ||
): void; | ||
/** | ||
* Checks `expectedStyle` is a subset of the elements computed style i.e. `window.getComputedStyle(element)`. | ||
* @example expect(element).toHaveComputedStyle({ width: '200px' }) | ||
*/ | ||
toHaveComputedStyle( | ||
expectedStyle: Record< | ||
Exclude< | ||
keyof CSSStyleDeclaration, | ||
| 'getPropertyPriority' | ||
| 'getPropertyValue' | ||
| 'item' | ||
| 'removeProperty' | ||
| 'setProperty' | ||
| number | ||
>, | ||
string | ||
expectedStyle: Partial< | ||
Record< | ||
Exclude< | ||
keyof CSSStyleDeclaration, | ||
| 'getPropertyPriority' | ||
| 'getPropertyValue' | ||
| 'item' | ||
| 'removeProperty' | ||
| 'setProperty' | ||
| number | ||
>, | ||
string | ||
> | ||
>, | ||
): void; | ||
/** | ||
|
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.
Not sure why I didn’t add this before. 👍