Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

How to use Custom Components as Props in rescript

$
0
0

For generic components, I like to use the first class module type :
It allows to enforce styling rules based on component props.

// Button.res
module type Icon = {
  @react.component
  let make: (~size: int=?, ~color: string=?, ~className: string=?) => React.element
}

@react.component
let make = (
    ~leftIcon: option<module(Icon)>=?,
   ...
) => {
       {leftIcon->Option.mapWithDefault(React.null, icon => {
        let module(Icon) = icon

        <span>
          <Icon size={24}  />
        </span>
      })}

}

// ReactIcons.res
module Md3dRotation = {
  @module("react-icons/md") @react.component
  external make: (~size: int=?, ~color: string=?, ~className: string=?) => React.element =
    "Md3dRotation"
}

<Button leftIcon={module(Md3dRotation)} />

Viewing all articles
Browse latest Browse all 2592

Trending Articles