GithubHelp home page GithubHelp logo

Comments (2)

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

Semantic UI + Styled Component

  • Semantic UIとかで用意されているコンポーネントのCSSをStyled Componentで上書きできる
    • たぶんMaterial UIとかでも同じようにいける

CSSの更新方法

styled()を使う

import styled from 'styled-component';

const Button = styled.button`
  color: palevioletred;
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

// こんな感じで更新できる
const TomatoButton = styled(Button)`
  color: tomato;
  border-color: tomato;
`;

Semantic UIでも同じように可能

import { Button } from 'semantic-ui-react';

const TomatoButton = styled(Button)`
  color: tomato;
  border-color: tomato;
`;

子要素にもCSSを適用するには

CardだけでなくCard.Contentまで

import { Card } from 'semantic-ui-react';

<StyledCard>
    <StyledCard.Content>
        {...}
    </StyledCard.Content>
</StyledCard>

const StyledCard = styled(Card)`
  cursor: pointer;
  // こんな感じで子要素のCSSを直接指定する
  &&&.ui.card .content {
    padding-right: 0;
    padding-left: 0;
  }
`;

分からないこと

  • <Card as={Link}>みたいなときにCSSが適用されなくなってしまうのでどう対処するのがスマートだったか?
    • すべてがタグにとして扱われてしまった
    • 以下のように対処した
//これはCSSがうまく適用されなかった
<StyledCard as={Link} to="/" />

//これだとOK
<Link>
    <StyledCard />
</Link>

参照

from sns-merng.

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

Styled ComponentでのCSSのoverride

  • Styled Componentでは!importantは使ってはいけない(なぜ?)
  • 代わりに&&&を使う(これはCSSの詳細度を上げる操作)
//これはダメ
const MyStyledComponent = styled(AlreadyStyledComponent)`
  color: palevioletred !important;
  font-weight: bold !important;
`

//これが正しい
const MyStyledComponent = styled(AlreadyStyledComponent)`
  &&& {
    color: palevioletred;
    font-weight: bold;
  }
`
  • ただ、そもそも!importantを使わない設計にすべき
    • 外部のデザインライブラリを使用するなら使う場面出てくるかも

参照

from sns-merng.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.