[TypeStyle] Compose CSS classes using TypeStyle
CSS Using Compose classes
2023-09-14 08:59:18 时间
We will demonstrate composing classes using the utility classes function. classes
is also what we recommend for theming. Using pure CSS classes means that the component consumers are free to customize the component using any technology (not just TypeStyle). classes
is also what is recommended for conditionally applied TypeStyle CSS class names.
import { style, classes } from 'typestyle'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; const fontSize = (value: number | string) => { const valueStr = typeof value === 'string' ? value : value + 'px'; return { fontSize: valueStr } }; const baseClassName = style( { color: 'green', }, fontSize(15) ); const errorClassName = style({ color: 'red' }, fontSize(12) ); const Section = ({children, hasError, className}: { children?: any, hasError?: boolean, className?: string }) => ( <div className={classes( baseClassName, className, hasError && errorClassName )}> {children} </div> ); const App = () => ( <div> <Section className={style({backgroundColor: 'pink'})}>Success</Section> <Section className={style({backgroundColor: 'yellow'})} hasError={true}>Error</Section> </div> ); ReactDOM.render( <App />, document.getElementById('root') );
相关文章
- CSS架构最佳实践:预测、重用、扩展、维护
- CSS - 使图片自适应
- CSS背景background详解,background-position详解
- CSS中div覆盖另一个div
- 【原】探索css换行
- 【原】css实现两端对齐的3种方法
- [CSS] Using box-shadow as background colors
- [TypeStyle] Add type safety to CSS using TypeStyle
- [CSS] Target Positional Elements Using *-Of-Type CSS pseudo-classes
- [CSS] Conditionally Apply Styles Using Feature Queries @supports
- [CSS] Control Image Aspect Ratio Using CSS (object-fit)
- [CSS3] Using CSS Combinators to Identify Siblings and Descendants in CSS
- Css 动画的回调
- css选择器优先级
- [CSS] Using transform: scale(0) for hiding element
- [CSS] Use CSS Variables Almost like Boolean Values with Calc (maintainable css)
- [CSS] Draw Simple Icons with CSS
- [HTML5] Focus management using CSS, HTML, and JavaScript
- [CSS] Conditionally Apply Styles Using Feature Queries @supports
- [CSS] Target empty elements using the :empty pseudo-class
- [AngularJS] ngAnimate using css. 1
- css 选择器学习笔记
- Atitit.html css 浏览器原理理论概论导论attilax总结
- css伪元素
- div背景css样式笔记
- 【CSS】PhotoShop 切图 ② ( PhotoShop 切片选择工具 | 清除切片 | 新建基于图层的切片 | 透明背景图片切图 | 根据参考线选择切片 )
- SharePoint远程调试CSS
- CSS_使用css布局
- Web前端开发CSS笔记
- 【CSS】css弹性布局、CSS hack_08
- CSS简洁导航条