在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):react-toolbox/react-toolbox开源软件地址(OpenSource Url):https://github.com/react-toolbox/react-toolbox开源编程语言(OpenSource Language):JavaScript 67.6%开源软件介绍(OpenSource Introduction):React Toolbox is a set of React components that implement Google's Material Design specification. It's powered by CSS Modules and harmoniously integrates with your webpack workflow, although you can use any other module bundler. You can take a tour through our documentation website and try the components live! Note: InstallationReact Toolbox can be installed as an npm package: $ npm install --save react-toolbox PrerequisitesReact Toolbox uses CSS Modules by default to import stylesheets written using PostCSS & postcss-preset-env features. In case you want to import the components already bundled with CSS, your module bundler should be able to require these PostCSS modules. Although we recommend webpack, you are free to use whatever module bundler you want as long as it can compile and require PostCSS files located in your Of course this is a set of React components so you should be familiar with React. If want to customize your components via themes, you may want to take a look to react-css-themr which is used by React Toolbox to make components easily themeable. Usage in Create React App ProjectsCreate React App does not allow to change the default configuration, so you need an additional build step to configure Follow these instructions to add Usage in Webpack Projects (Not Create React App)npm install postcss-loader --save-dev
npm install postcss postcss-preset-env postcss-calc --save Configure webpack 1.x loader for .css files to use postcss: {
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss?sourceMap&sourceComments',
],
}, Declare plugins to be used by postcss (as part of webpack's config object): // webpack.config.js
postcss: () => {
return [
/* eslint-disable global-require */
require('postcss-preset-env')({
stage: 0, // required to get all features that were from cssnext without enabling them one by one
features: {
'custom-properties': {
preserve: false, // required to output values instead of variables
},
'color-mod-function': true, // required to use color-mod()
}
}),
require('postcss-calc'), // required as postcss-preset-env doesn't have a reduce calc() funtion that cssnext did
/* eslint-enable global-require */
];
}, Configure webpack 2.x or 3.x loader for .css files to use postcss: // webpack.config.js
{
test: /\.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true, // default is false
sourceMap: true,
importLoaders: 1,
localIdentName: "[name]--[local]--[hash:base64:8]"
}
},
"postcss-loader"
]
} Basic usageIn this minimal example, we import a import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'react-toolbox/lib/button';
ReactDOM.render(
<Button label="Hello World!" />,
document.getElementById('app')
);
Take into account that any required style will be included in the final CSS so your final CSS would include Importing componentsFirst let's take a look on how the components are structured in the project. The components folder contains a folder for each component or set of related components. For example, the
As you can see in the previous block, each folder includes: a Javascript file for each component/subcomponent; a README with documentation, an index Javascript file that imports and injects styles and dependencies for you, a default theme PostCSS/preset-env stylesheet and a config.css with configuration variables (CSS Custom Properties). Depending on whether you want the styles to be directly bundled or not, you can import components in two different ways. Bundled componentIf you import from the index file, the imported component comes with all dependencies and themes already required and injected for you. This means that the CSS for each dependency will be bundled in your final CSS automatically and the component markup includes the classnames to be styled. For example: import { AppBar } from 'react-toolbox/lib/app_bar'; Raw componentIf you import from the component definition, the imported component is bundled with its dependencies, but it does not include any styles. This means no CSS will be bundled, and the component markup will not include any classname. It's your responsibility to provide a theme to the component to be properly styled. You can do so via properties or context. For example: import { AppBar } from 'react-toolbox/lib/app_bar/AppBar.js'; Customizing componentsEvery component accepts a If the component already has a theme injected, the properties you pass will be merged with the injected theme. In this way, you can add classnames to the nodes of a specific component and use them to add or to override styles. For example, if you want to customize the import React from 'react';
import { AppBar } from 'react-toolbox/lib/app_bar';
import theme from './PurpleAppBar.css';
const PurpleAppBar = (props) => (
<AppBar {...props} theme={theme} />
);
export default PurpleAppBar; .appBar {
background-color: #800080;
} In this case we are adding styles to a specific instance of an If the component has no styles injected, you should provide a theme object implementing the full API. You are free to require the CSS Module you want but take into account that every classname is there for a reason. You can either provide a theme via prop or via context as described in the next section. Customizing all instances of a component typeInstall react-css-themr with Create a CSS Module theme style file for each component type, for example for # /css/button.css
.button {
text-transform: uppercase;
} Create a theme file that imports each component's custom theme style under the special theme key listed in that widgets's documentation, i.e.: # theme.js
import RTButton from './css/button.css';
import RTDatePicker from './css/datepicker.css';
export default {
RTButton, RTDatePicker,
}; Wrap your component tree with ThemeProvider at the desired level in your component hierarchy. You can maintain different themes, each importing differently styled css files (i.e. import React from 'react';
import { ThemeProvider } from 'react-css-themr';
import theme from './theme';
class App extends React.Component {
render() {
return (
<ThemeProvider theme={theme}>
<div>
...
</div>
</ThemeProvider>
);
}
}
export default App; Theming (configuration variables)You can apply theming in multiple ways. First of all, you have to understand that React Toolbox stylesheets are written using PostCSS with postcss-preset-env features and use CSS Custom Properties from the config files we saw earlier. In addition, there are some global CSS Properties imported by each component: colors and variables. You can override both the global and component-specific variables to get the results you want using one of the methods below. Settings configuration variables in JavaScriptYou can override both the global and component-specific CSS Custom Properties at build-time by supplying an object with these variable names and your desired values to the PostCSS // This can also be stored in a separate file:
const reactToolboxVariables = {
'color-text': '#444548',
/* Note that you can use global colors and variables */
'color-primary': 'var(--palette-blue-500)',
'button-height': '30px',
};
// webpack's config object: (webpack.config.js)
const config = {
...
postcss: () => {
return [
/* eslint-disable global-require */
require('postcss-preset-env')({
stage: 0, // required to get all features that were from cssnext without enabling them one by one
features: {
'custom-properties': {
preserve: false, // required to output values instead of variables
importFrom: reactToolboxVariables, // see postcss-preset-env for config options
},
'color-mod-function': true, // required to use color-mod()
}
}),
require('postcss-calc'), // required as postcss-preset-env doesn't have a reduce calc() funtion that cssnext did
/* optional - see next section */
require('postcss-modules-values'),
/* eslint-enable global-require */
];
},
} Settings configuration variables using CSS Module ValuesInstead of using a JavaScript object for variables, you can use CSS Module Values ( CSS Module Values also offer the advantage that importing a css file with @value declarations makes these values properties of the imported style object, i.e.: # variables.css
@value buttonPrimaryBackgroundColor: #9c3990; import styleVariables from './css/variables.css';
styleVariables.buttonPrimaryBackgroundColor In this demo project, modules-values-extract utility is used to extract all @values with dashes in their name from all css files in the
Roboto Font and Material Design IconsReact Toolbox assumes that you are importing Roboto Font and Material Design Icons. In order to import the fonts for you, we'd need to include them in the CSS which is considered a bad practice. If you are not including them in your app, go to the linked sites and follow the instructions. Server Side RenderingThe only requirement for SSR is to be able to require ES6 and CSS Modules in the backend. To make it possible you can check projects like CSS Modules register hook or Webpack Isomorphic tools. Also, make sure you can import from ExamplesFor now we have a repository example demonstrating configuration and some basic customization. For now it's not using SSR rendering but it shouldn't be difficult to implement an example so it will come soon. Feel free to PR your example project or to add some use cases to the repository: Another 2.x demo project is https://github.com/alexhisen/mobx-forms-demo TypeScriptTypeScript external module definition files are included, and should not require any manual steps to utilize. They will be picked up by the TypeScript compiler when importing from the npm package. Note that to comply with the official recommendation for npm typings, a triple-slash reference to Authors and ContributorsThe project is being initially developed and maintained by Javier Velasco and Javier Jiménez and the contribution scene is just getting warm. We want to create reference components so any contribution is very welcome. To work in the project you'd need a To start the documentation site locally, you'll need to install the dependencies from both the main package and the docs subproject:
Local documentation will then be available at ExtensionsWe don't officially support components that are not covered by Google Material Design. If you want to implement some complementary component feel free to open a PR adding your a link in this section: on github: react-toolbox-additions and on npm: react-toolbox-additions. Form generation and validation using React-Toolbox form widgets - mobx-schema-form SupportBackersSupport us with a monthly donation and help us continue our activities. [Become a backer]
2023-10-27 2022-08-15 2022-08-17 2022-09-23 2022-08-13 |
请发表评论