Skip to content

Commit

Permalink
Support string templates in Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerclotet committed Aug 8, 2019
1 parent 0168a6d commit 9a517e1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
40 changes: 40 additions & 0 deletions docs/src/pages/styles/advanced/StringTemplates.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { jssPreset, StylesProvider, makeStyles } from '@material-ui/styles';
import { create } from 'jss';
import jssTemplate from 'jss-plugin-template';

const jss = create({
plugins: [jssTemplate(), ...jssPreset().plugins],
});

const useStyles = makeStyles({
root: `
background: linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%);
border-radius: 3px;
font-size: 16px;
border: 0;
color: white;
height: 48px;
padding: 0 30px;
box-shadow: 0 3px 5px 2px rgba(255, 105, 135, 0.3);
`,
});

function Child() {
const classes = useStyles();
return (
<button type="button" className={classes.root}>
String templates
</button>
);
}

function StringTemplates() {
return (
<StylesProvider jss={jss}>
<Child />
</StylesProvider>
);
}

export default StringTemplates;
2 changes: 1 addition & 1 deletion packages/material-ui-styles/src/makeStyles/makeStyles.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export default function makeStyles<
Props extends {} = {},
ClassKey extends string = string
>(
styles: Styles<Theme, Props, ClassKey>,
styles: Styles<Theme, Props, ClassKey> | Record<ClassKey, string>,
options?: Omit<WithStylesOptions<Theme>, 'withTheme'>,
): StylesHook<Styles<Theme, Props, ClassKey>>;
16 changes: 16 additions & 0 deletions packages/material-ui-styles/src/makeStyles/makeStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ describe('makeStyles', () => {
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'MuiTextField-root' });
});

it('should apply string styles', () => {
const useStyles = makeStyles({ root: `display: flex;` });
const StyledComponent = () => {
const classes = useStyles();
return <div className={classes.root} />;
};

mount(
<StylesProvider sheetsRegistry={sheetsRegistry} sheetsCache={new Map()}>
<StyledComponent />
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].rules.raw, { root: 'display: flex;' });
});

describe('overrides', () => {
it('should support the overrides key', () => {
const useStyles = makeStyles(
Expand Down
20 changes: 18 additions & 2 deletions packages/material-ui-styles/test/styles.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import {
createStyles,
makeStyles,
WithStyles,
withStyles,
withTheme,
WithTheme,
WithStyles,
makeStyles,
} from '@material-ui/styles';
import Button from '@material-ui/core/Button';
import { Theme } from '@material-ui/core/styles';
Expand Down Expand Up @@ -450,6 +450,22 @@ function forwardRefTest() {
const root2 = styles.root2;
}

{
// It works with string styles
interface testProps {
foo: boolean;
}

const useStyles = makeStyles({
root: `
display: flex;
`,
});
const styles = useStyles({ foo: true });
// $ExpectType string
const root = styles.root;
}

{
// If there are no props, use the definition that doesn't accept them
// https://github.com/mui-org/material-ui/issues/16198
Expand Down

0 comments on commit 9a517e1

Please sign in to comment.