@digitalroute/theme
v7.18.1
Published
DigitalRoute's theme.
Downloads
99
Keywords
Readme
When to use the DR-Theme
- When styling Ant Design components with styles that should apply to all components and not just specific ones.
- When we start to repeat ourselfs in the styling of things e.g you specifing the same color for the 2:nd or 3:rd time.
- When .....
How
Please note, this repository uses semantic release. Please read about it in the Conventional Commits page in Confluence.
- Run
npm install
to install dependencies - Make your changes
- Make sure your changes are visible in the storybook (add a new story if necessary) and hasn't broken any of the existing ones:
npm run storybook
- When you're satisfied with the result, stage your changes using
git add
and then commit usingnpm run commit
. Add a nice descriptive comment about the changes. - Push to your feature branch and create a PR.
Peer dependencies
Currently this module expects to find peer dependencies for react (^17.0.0) and antd (4.16.3) in your app.
Theme With react-jss
This is an implementation of theming react apps using react-jss. For smooth integration with antd, we have a custom webpack loader which parses the antd's LESS files and extracts all variables into the a theme which is provided in the app for usage when styling components. Hence, any changes of antd's components, and theme variables in general, is accomplished through setting the different LESS variables exposed by antd found in eg. ./src/theme.less.
One of the most important benefits of JSS is that it promotes components to be contained in a single file. This encourages and ensures pain-free future refactorings without also having to deal with external stylesheets. A list of benefits, as well as more useful information can be found att jss' website. The integration with antd also ensures that theming of the existing components is done seamlessly.
Whilst LESS is used in this module due to antd, there is no requirement for any consuming library to use LESS. Preferably, the functionality provided by JSS will be more than enough even for advanced stylerz.
The module exposes two utility functions for consumers to use, shortly explained below. Simple but complete examples can be found in ./stories/index.jsx. To show them live, run npm install && npm run storybook
.
Whilst we haven't yet published this to an NPM registry, you can use the module by adding the following dependency:
"@digitalroute/theme": "git+ssh://[email protected]:7999/daz/dr-theme.git#<tag>"
withTheme(app: Component)
import { withTheme } from '@digitalroute/theme';
class App extends React.Component {
render() {
<SomeComponent />
...
}
}
const ThemedApp = withTheme(App);
useStyles
useStyles are not exported by dr-theme, but to provide an example how to use it below, theme is injected by withTheme in the top of the application as shown above.
import { createUseStyles } from "react-jss";
// Styles only need to be a function when theme is used, object otherwise
const styles = (theme) => ({
yolo: {
display: 'flex',
flexDirection: 'column',
color: theme.primaryColor,
...
},
...
});
const useStyles = createUseStyles(styles);
export const SomeComponent = () => {
const classes = useStyles();
return <div className={classes.yolo}/>
};
[LEGACY (Deprecated in react-jss v10)] Creates a higher-order-component (HOC) of your app that sets up and provides the theme. Typically, this is used near the root-component in an application and applied on a single component in the component-tree. Preferred to use only with class component, as useStyles hooks are replacing this.
withStyles(styles: func)
Creates a new function which in turn accepts a component to turn into a styled (HOC) version, through which the defined css-classes defined in styles are provided as a the "classes"-prop:
import { withStyles } from '@digitalroute/theme';
import { WithStylesProps } from '@digitalroute/theme';
const styles = (theme) => ({
yolo: {
display: 'flex',
flexDirection: 'column',
color: theme.primaryColor,
...
},
...
});
interface Props extends WithStylesProps<typeof styles> {}
const SomeComponent = ({ classes }:Props) => (
<div className={classes.yolo}>
...
</div>
);
export default withStyles(styles)(SomeComponent);
TODOs
We'll eg. be looking into reducing the dist size.