Marc Knaup
10/21/2020, 2:28 AMval styledComponent = styled(someFunctionalComponent) { css { … } }
. Took me a few hours to figure that one out 😮
fun <P> styled(component: FunctionalComponent<P>, handler: StyledElementBuilder<P>.(props: P) -> Unit): FunctionalComponent<P> where P : RProps, P : WithClassName {
val buildStyled = styled(component.unsafeCast<RClass<P>>())
val styledComponent = { props: P ->
buildElement {
buildStyled {
val dynamicAttrs = attrs.asDynamic()
val dynamicProps = props.asDynamic()
props.getOwnPropertyNames().forEach { propertyName ->
dynamicAttrs[propertyName] = dynamicProps[propertyName]
}
handler(props)
}
}
}
if (js("process.env.NODE_ENV !== 'production'") as Boolean) {
val displayName = component.asDynamic().displayName as String?
if (displayName != null)
styledComponent.asDynamic().displayName = "styled.$displayName"
}
return styledComponent
}
Big Chungus
10/21/2020, 6:54 AMMarc Knaup
10/21/2020, 9:22 AMRBuilder
.
If you create a component you need it as an RClass
instance. Otherwise you can’t use it with high-order components like memo(…)
or styled(…)
.Big Chungus
10/21/2020, 9:24 AMMarc Knaup
10/21/2020, 9:24 AMBig Chungus
10/21/2020, 9:25 AMbnorm
10/21/2020, 7:50 PMmemo(…)
should be pretty easy to support as an annotation property. There’s probably also a way the library could provide the backing RClass for combining with other libraries.
But you are correct, right now the library only supports the most basic of use cases but mainly because I don’t have a lot of experience with React and I haven’t needed anything more. Very happy to take suggestions on how to make the library better.Marc Knaup
10/21/2020, 7:52 PMstyled
and memo
are just 2 of unlimited potential use cases.Marc Knaup
10/21/2020, 7:52 PMstyled
or memo
.
Everybody can define such wrapping definitions. They’re called high-order components.Marc Knaup
10/21/2020, 7:53 PMMarc Knaup
10/21/2020, 7:53 PMbnorm
10/21/2020, 8:02 PMexternal interface ComponentProps : RProps
from the function parameters and generates a val COMPONENT: RClass<ComponentProps>
which contains the body of the function rewritten to use the properties. The original function is then transformed to call invoke
on the resulting COMPONENT
. So it is very possible the library could somehow provide access to these generated interfaces and properties.Marc Knaup
10/21/2020, 8:03 PMbnorm
10/21/2020, 8:05 PMMarc Knaup
10/21/2020, 8:05 PMbnorm
10/21/2020, 8:11 PMMarc Knaup
10/21/2020, 8:12 PMfun RBuilder.Hello(name: String) {
I don’t use that pattern at all 🤔bnorm
10/21/2020, 8:17 PMMarc Knaup
11/12/2020, 5:05 PMstyled
. It’s just not a good fit for Kotlin.
High-order-components in general are problematic in Kotlin. I’ll avoid them now.
That means <http://RBuilder.xyz|RBuilder.xyz>
is the best option now 🙂