如何在样式组件中实现CSS混合?

时间:2020-10-17 07:38:14

标签: css reactjs styled-components

我在项目中使用了一些基类,例如text-lg,text-md ...,。container等。 通常,我以这种方式编写类:

public class MainClass {
   public static void main(String[] args) {
        ArrayList<Area> areasList = new ArrayList<>(10);
        Area math = new Area("math");
        areasList.add(math);
        math.addTeacher(new Teacher("Mr. Freeman", 3500f));
        math.addTeacher(new Teacher("Mr. Greedy", 3200f));
    }
}

class Area {
    private String name;
    private List<Teacher> teachersList;

    public Area(String name) {
        this.name = name;
        this.teachersList = new ArrayList<Teacher>(10); //define what type of list do you want
    }

    public String getName() {
        return name;
    }

    public List<Teacher> getTeachersList() {
        return teachersList;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addTeacher(Teacher teacher) {
        this.teachersList.add(teacher);
    }

    public void removeTeacher(Teacher teacher) {
        this.teachersList.remove(teacher);
    }

}

class Teacher {
    String name;
    float salary;

    public Teacher(String name, float salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
}

如何使用样式化组件执行类似的操作?为这些类创建mixin,还是仅混合使用样式组件和css类?

1 个答案:

答案 0 :(得分:0)

您可以使用css helper创建基类并将其组合。

string uri = blob.Uri.AbsoluteUri;
FileInfo fileInfo = new FileInfo(uri);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
Console.WriteLine(ws.Cells[1,1].Value.ToString());

或者,如果您已经拥有一个样式表(例如,诸如Bootstrap之类的CSS库),则可以使用attrs将它们组合在一起。这样可以节省您不得不用JS重写CSS的时间。在此处阅读官方文档:https://styled-components.com/docs/faqs#can-i-use-css-frameworks

const container = css`
  padding: 20px;
`;
const article = css`
  max-width: 1920px;
`;
const textLg = css`
  font-size: 3rem;
`;

const CustomDiv = styled.div`
  ${container + article + textLg}

  /* other styles here */
  background: black; 
  color: white;
`;

基本上,输出将是具有const CustomDiv = styled.div.attrs(props => ({ className: "article text-lg container", }))` /* other styles here */ background: black; color: white; `; backgroundcolor类的articletext-lg CSS属性的定制生成类在您的样式表中定义

HTML codes

CSS codes