Expose theme objects but use CSS Variables internally in React

栏目: IT技术 · 发布时间: 4年前

内容简介:This tiny 500byte (not gzipped) component allows JS theme objects to be passed into your component where they're converted into CSS Variables for internal use.Your component can now make use of the following CSS Variables:

React CSS Theming (with JS objects)

This tiny 500byte (not gzipped) component allows JS theme objects to be passed into your component where they're converted into CSS Variables for internal use.

Demo

https://codesandbox.io/s/react-js2css-theme-demo-s6xlx

Why?

  • Switching CSS Variables for themes is unpleasant for users of your component, JS objects are easier to deal with.

  • CSS Variable changes are much faster than React re-renders via Context which is what CSS-in-JS solutions do.

  • If you care about performance and bundle size and want to avoid CSS-in-JS solutions altogether you can still provide a nice JS theme object interface to your users.

Install

yarn add react-js2css-theme

Usage

import JSToCSSTheme from 'react-js2css-theme';

const yourTheme = {
  background: 'black',
  textColor: 'white',
  fontFamily: '"Roboto", sans-serif',
  button: {
    padding: '10px',
  },
};

<JSToCSSTheme namespace="xx" theme={yourTheme}>
  <YourComponent />
</JSToCSSTheme>;

Your component can now make use of the following CSS Variables:

:root {
  --xx-background: black;
  --xx-text-color: white;
  --xx-font-family: 'Roboto', sans-serif;
  --xx-button-padding: 10px;
}

Now try changing the theme object and watch your component's theme change :)

Requirements

useMemo

以上所述就是小编给大家介绍的《Expose theme objects but use CSS Variables internally in React》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

鸟哥的Linux私房菜 基础学习篇(第二版)

鸟哥的Linux私房菜 基础学习篇(第二版)

鸟哥 / 人民邮电出版社 / 2007-9 / 65.00元

《鸟哥的Linux私房菜基础学习篇(第二版)》全面而详细地介绍了Linux操作系统。全书分为5个部分:第一部分着重说明Linux的起源及功能,如何规划和安装Linux主机;第二部分介绍Linux的文件系统、文件、目录与磁盘的管理;第三部分介绍文字模式接口shell和管理系统的好帮手shell脚本,另外还介绍了文字编辑器vi和vim的使用方法;第四部分介绍了对于系统安全非常重要的Linux账号的管理......一起来看看 《鸟哥的Linux私房菜 基础学习篇(第二版)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具