ingred-ui icon indicating copy to clipboard operation
ingred-ui copied to clipboard

styled-components を v6 にする

Open takurinton opened this issue 1 year ago • 14 comments

現状、型が export されてなかったりして上げられないので一旦待機。

関連

  • https://github.com/voyagegroup/ingred-ui/pull/1307
  • https://github.com/voyagegroup/ingred-ui/pull/1347

takurinton avatar Aug 02 '23 22:08 takurinton

styled-components v6 になって ThemedStyledProps が export されなくなった。 https://github.com/voyagegroup/ingred-ui/blob/master/src/utils/spacer.ts#L1

これは ExecutionContext という型が export されてるので、それを使って再現可能。 ThemedStyledProps は型引数を2つ(マップしたい型と theme)受け取っていたが、ExecutionContext が theme の方はいい感じに当ててくれるのでマップしたい型だけ渡すようにした。

import { ExecutionContext } from "styled-components";

export type ThemedStyledProps<P> = P & ExecutionContext;

takurinton avatar Aug 06 '23 23:08 takurinton

v5 まで存在していた ThemedStyledProps の定義はここにある https://github.com/DefinitelyTyped/DefinitelyTyped/blob/ca9f5035eef6451d5389528b8813e52d2ced2c05/types/styled-components/v3/index.d.ts#L12-L16

takurinton avatar Aug 06 '23 23:08 takurinton

react-transition-group を使ってる箇所で、nodeRef を渡そうとするとエラーが出る。 nodeRef じゃなくて ref にしたら型エラーがなくなる。

予期された型は、型 'IntrinsicAttributes & PolymorphicComponentProps<"web", Substitute<(Pick<TimeoutProps<HTMLElement | undefined> & { classNames?: string | CSSTransitionClassNames | undefined; }, keyof TimeoutProps<...>> | Pick<...>) & RefAttributes<...>, CSSTransitionProps>, void, void, {}, {}>' に対してここで宣言されたプロパティ 'nodeRef' から取得されています

takurinton avatar Aug 07 '23 00:08 takurinton

npx tsc の結果(一部抜粋)

Overload 1 of 2, '(props: PolymorphicComponentProps<"web", Substitute<(Pick<TimeoutProps<HTMLElement | undefined> & { classNames?: string | CSSTransitionClassNames | undefined; }, keyof TimeoutProps<...>> | Pick<...>) & RefAttributes<...>, CSSTransitionProps>, void, void, {}, {}>): Element', gave the following error. Type 'RefObject<HTMLDivElement>' is not assignable to type 'Ref | undefined'. Overload 2 of 2, '(props: Substitute<(Pick<TimeoutProps<HTMLElement | undefined> & { classNames?: string | CSSTransitionClassNames | undefined; }, keyof TimeoutProps<...>> | Pick<...>) & RefAttributes<...>, CSSTransitionProps>): ReactElement<...> | null', gave the following error. Type 'RefObject<HTMLDivElement>' is not assignable to type 'Ref | undefined'. 16 nodeRef={nodeRef}

おそらくここらへんの話 https://github.com/styled-components/styled-components/issues/4076

takurinton avatar Aug 07 '23 00:08 takurinton

手元で型のテスト書いてみたら普通に通ってるしよくわからんかった

image

takurinton avatar Aug 10 '23 22:08 takurinton

これは一旦こんな感じに書くと通るのでスルーでいいかな。 https://github.com/voyagegroup/ingred-ui/compare/styled-components-v6#diff-bfed18578c6277356dc2718fa9149b6477dc8d161f71db8348a6955e882cb907R6-R9

takurinton avatar Aug 10 '23 22:08 takurinton

ちなみに nodeRef の定義は明示的に書かなくてもいい(書いておくけど)

takurinton avatar Aug 10 '23 22:08 takurinton

spacing の部分でランタイムエラーを吐いた、dependabot の PR のときと同じだ image

takurinton avatar Aug 11 '23 04:08 takurinton

react-select のランタイムエラーはこれ https://github.com/styled-components/styled-components/issues/4074

以下のコンポーネントを styled-components で装飾して渡すと menuIsOpen が true の時にエラーになる

  • ValueContainer(ingred-ui に該当箇所なし)
  • Option(ingred-ui に該当なし)
  • MenuList

takurinton avatar Aug 13 '23 20:08 takurinton

react-select 側で spacing が参照できない箇所が壊れてるっぽいけど、styled-components でどう扱ってるのかわからないのと、対応策がこちらにあるのか styled-components 自体にあるのかの判断が現状できてない(そもそも theme が壊れてるようにも見える)

takurinton avatar Aug 13 '23 21:08 takurinton

react-select は外から注入したコンポーネントはここで上書きされるはずだけどされてなさそう? https://github.com/JedWatson/react-select/blob/master/packages/react-select/src/components/index.ts#L141-L151

ingred-ui のケースだと、ここで MenuList が上書きされてくれないといけないのに、されないで react-select の MenuList が呼ばれてしまっているのでエラーになってるような雰囲気

takurinton avatar Aug 13 '23 22:08 takurinton

react-select のエラーの件は styled-components v6.0.0-beta.1 以降で壊れてる リリースノート: https://github.com/styled-components/styled-components/releases/tag/v6.0.0-beta.1

takurinton avatar Aug 13 '23 22:08 takurinton

react-select は外から注入したコンポーネントはここで上書きされるはずだけどされてなさそう?

node_modules の下で print debug してみた感じ、そういうのではなさそうだった。 displayName: Styled(MenuList) になっていたし、JSX.Element ぽいものが格納されていた(react-select のデフォルトだったら [function MenuList] とかになるはず)

takurinton avatar Aug 13 '23 22:08 takurinton

ひとまず現在地のメモを書いた。react-select の問題が解決したら v6 に上げてもいいかもなー(PR は作ったのでマージされれば対応できそう) https://memo.takur.in/tech/frontend/styled-components-v6.html

takurinton avatar Aug 14 '23 05:08 takurinton