Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style-components #197

Open
hysryt opened this issue Mar 7, 2022 · 1 comment
Open

style-components #197

hysryt opened this issue Mar 7, 2022 · 1 comment

Comments

@hysryt
Copy link
Owner

hysryt commented Mar 7, 2022

https://styled-components.com/

React用のライブラリ
CSS in JS

@hysryt
Copy link
Owner Author

hysryt commented Mar 7, 2022

インストール

npx create-react-app my-app
cd my-app
npm install --save style-components

import styled from 'styled-components';

const Button = styled.button`
  background: red;
`;

function App() {
  return (
    <div className="App">
      <Button>button</Button>
    </div>
  );
}

export default App;

styled.button でReactコンポーネントを生成している。

この構文はタグ付きテンプレート文字列リテラルというJavaScriptに元々備わっている構文。

プロパティによってスタイルを変える

import styled, { css } from 'styled-components';

const Button = styled.button`
  background: red;
  ${props => props.primary && css`
    background: blue;
  `}
`;

function App() {
  return (
    <div className="App">
      <Button>button</Button>
      <Button primary>button</Button>
    </div>
  );
}

export default App;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant