1

将近 2 年后,我将重返编码领域。我对反应还很陌生。我真的在摸索如何在反应中使用伪选择器。我遇到了许多使用 Radium 的答案,但只是我无法在我的代码中实现这一点。任何帮助表示赞赏。

下面是 React 组件:

import React from "react";
import Radium from 'radium';

const styles = {
    input[type="text"] : {
    box-sizing: border-box;
    width: 100%;
    height: calc(4em + 1px);
    margin: 0 0 1em;
    padding: 1em;
    border: 1px solid #ccc;
    background: #fff;
    resize: none;
    outline: none;
  },

  input[type="text"][required]:focus {
    border-color: #00bafa;
  },
  input[type="text"][required]:focus + label[placeholder]:before {
    color: #00bafa;
  },
  input[type="text"][required]:focus + label[placeholder]:before,
  input[type="text"][required]:valid + label[placeholder]:before {
    transition-duration: 0.2s;
    transform: translate(0, -1.5em) scale(0.9, 0.9);
  },

  input[type="text"][required]:invalid + label[placeholder][alt]:before {
    content: attr(alt);
  },

  input[type="text"][required] + label[placeholder] {
    display: block;
    pointer-events: none;
    line-height: 1em;
    margin-top: calc(-3em - 2px);
    margin-bottom: calc((3em - 1em) + 2px);
  },

  input[type="text"][required] + label[placeholder]:before {
    content: attr(placeholder);
    display: inline-block;
    margin: 0 calc(1em + 2px);
    padding: 0 2px;
    color: #898989;
    white-space: nowrap;
    transition: 0.3s ease-in-out;
    background-image: linear-gradient(to bottom, #fff, #fff);
    background-size: 100% 5px;
    background-repeat: no-repeat;
    background-position: center;
  }

};

const GmailInput = () => {
 return (
    <div>
      <form>
       <input type="text" />
       <label placeholder="Type your Email" />
      </form>
    </div>
 );
 };

 export default Radium(GmailInput);
4

1 回答 1

0

按照语法..

import React from "react";
import { StyleRoot } from "radium";

const GmailInput = props => {
  const styles = {
    boxSizing: "border-box",
    width: "100%",
    height: "calc(4em + 1px)",
    margin: "0 0 1em",
    padding: "1em",
    border: "1px solid #ccc",
    background: "#fff",
    resize: "none",
    outline: "none",
    ":hover": {
      borderColor: "#00bafa"
    }
  };

  return (
    <StyleRoot>
      <div>
        <form style={styles}>
          <input type="text" placeholder="Type your Email"></input>
        </form>
      </div>
    </StyleRoot>
  );
};

export default GmailInput;

理解,

  1. 您必须将所有变量放在组件的主体中。

  2. 当您使用基于功能的组件时,{StyleRoot} 是使用 Radium 的伪样式所必需的。

  3. 请了解如何在 React 中使用 JavaScript 进行样式设置。虽然样式属性是 CSS,但是当你用 JS 编写时,那些只是 JS 对象。React 编译器将它们编译成 CSS 样式。

  4. 样式需要JSX样式属性,请参见样式={styles} ,样式是此处的变量。

  5. Radium(Com_name) 用于基于类的组件。它不需要基于功能的组件,但 StyleRoot 中的 return 是强制性的。

  6. 带“-”的 CSS 语法,如 box-sizing,将通过 camelCasing 输入,如 boxSizing。

(这里没有写你所有的css属性,只是为了展示概念。这个语法已经编译成功)

于 2019-06-26T16:22:15.760 回答