[TypeStyle] Style CSS pseudo-classes using TypeStyle with $nest

本文介绍如何使用TypeStyle库来实现类似于CSS伪类(如:focus、:hover、:active)的效果,通过React和TypeStyle结合的方式展示了字体大小变化及背景颜色切换等样式调整。

TypeStyle is a very thin layer on top of CSS. In this lesson we show how to change styles based on pseudo classes e.g. :focus :hover and :active which matches very closely with what you would write with raw CSS.

 

import { style, media } 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 className = style(
    {
        color: 'red',
        transition: 'font-size 0.2s',
        $nest: {
            '&:focus': {backgroundColor: 'green'},
            '&:hover': fontSize(50),
            /** iphone */
            '@media screen and (-webkit-min-device-pixel-ratio: 2)': {
                backgroundColor: 'blue'
            }
        }
    },
    media({maxWidth: 700, minWidth: 500}, fontSize(20)),
    media({minWidth: 701}, fontSize(30)),
    media({maxWidth: 499}, fontSize(15))
);

ReactDOM.render(
    <button className={className}>
        Hello Typestyle
    </button>,
    document.getElementById('root')
);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值