LoginSignup
0
0

More than 5 years have passed since last update.

hyperapp用のstyled-componentsみたいなのをつくった

Last updated at Posted at 2018-12-22

目的

hyperappでstyled-componentsみたいにスタイルを書いてみたかった。

hyperapp

こちらをごらんください
あと、picostyleというスタイル書くやつがすでにあるので、ごらんください

作ったもの

できること

styled-componentsみたいに書ける

const Element = styled.div`
  background-color: yellow;
`;

<Element>Yellow</Element>

独自のattributesを渡すこともできる

const Element = styled.div<{ color: string }>`
  background-color: ${({ color }) => color};
`;

<Element color="blue">Yellow</Element>

div以外もある

const Button = styled.button`
  background-color: yellow;
`;

できないこと

擬似要素→できるようにした

const Paragraph = styled.p`
  &:before {
    content: "> "
  }
`;

アニメーション

const Animation = styled.div`
  @keyframes {
    0% {
      color: yellow;
    }
    100% {
      color: blue;
    }
  }
`;

wrap関数でさらにラップしたエレメントを作る→できるようにした

const Original = styled.div`
  background-color: yellow;
`;

const Wrapped = wrap(Original)`
  font-size: 32px;
`;

ただし、hyperappのコンポーネントに対するスタイルはうまくいかないので注意。

しくみ

styled-componentsみたいに要素にユニークなidを振るようにして、そのidに合致するようにcssを吐く。

code.tsx
const Div = styled.div`
  color: yellow;
`;

html
<div data-style="hhygaw">foo</div>
<style>
*[data-style=hhygaw] {
  color: yellow;
}
</style>

性能とかは何も考えていないので、再描画の度に<style>を書き換えている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0