Skip to content

Instantly share code, notes, and snippets.

@azu
Last active March 3, 2016 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azu/d5e92de127f76545ffc2 to your computer and use it in GitHub Desktop.
Save azu/d5e92de127f76545ffc2 to your computer and use it in GitHub Desktop.
React ComponentとCSSの継承
"use strict";
import React from "react";
import classNames from "classnames";
export default class ActionButton extends React.Component {
render() {
return <button {...this.props}
className={classNames("ActionButton", this.props.className)}>
{this.props.children}
</button>
}
}

Components

分類

  • project/
    • プロジェクト固有のUI
  • ui/
    • UIを抽象化したもの
    • projectがuiを継承したプロジェクト固有UIを実装する

例) ActionButton

ActionButton を継承したコンポーネントにはActionButtonというクラスが付加される。

import React from "react";
import ActionButton from "../../ui/ActionButton";
import Icon from "../../ui/Icon"
export default class BookmarkButton extends React.Component {
    render() {
        return <ActionButton className="BookmarkButton">
            <Icon src="./assets/buttons/bookmark-icon.png"/>
            <span className="BookmarkButton-title">ブックマーク</span>
        </ActionButton>
    }
}

この時 BookmarkButtonclass属性値は

  • BookmarkButton
  • ActionButton

となる(class="ActionButton BookmarkButton")

  • ActionButton 共通のスタイルは ui/ActionButton.css
  • BookmarkButton 固有のスタイルは components/BookmarkButton.css

それぞれ書くことで、コンポーネントとCSSの継承を関連づけている。

.ActionButton {
    padding: 0.1em 0.5em;
    margin: 0.3em;
}

.BookmarkButton {
    background-color: black;
}

.BookmarkButton-title {
    color: white;
    vertical-align: middle;
}

という感じでスタイルも共通と固有がファイルとして分離できる?

@azu
Copy link
Author

azu commented Mar 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment