Skip to content

Add example usage #1

@jonlaing

Description

@jonlaing

Hi, thanks for putting the work into these bindings! I'm new to Rescript so it wasn't immediately obvious to me how to use it, but I was able to figure it out by reading your code and playing around with it for a bit. Anyway, I thought it might be helpful to other people if there was at least one example of using it.

Also, you might want to update the README to reflect what bindings are working. Seems to me that at least Animated.View and withSpring are working as intended.

Anyway, here is the little toy example I wrote to try out the bindings. I have it working in Expo 44.0 on both iOS and Android:

open ReactNative

let styles = {
  open Style

  StyleSheet.create({
    "container": viewStyle(
      ~alignItems=#center,
      ~justifyContent=#center,
      ~borderRadius=4.0,
      ~position=#relative,
      ~backgroundColor="transparent",
      (),
    ),
    "presser": viewStyle(~flex=1.0, ()),
    "backer": viewStyle(
      ~position=#absolute,
      ~top=0.0->dp,
      ~left=0.0->dp,
      ~right=0.0->dp,
      ~bottom=0.0->dp,
      ~borderRadius=4.0,
      (),
    ),
    "textContainer": viewStyle(
      ~paddingVertical=5.0->dp,
      ~paddingHorizontal=10.0->dp,
      (),
      ~backgroundColor="transparent",
    ),
  })
}

@genType("Selectable") @react.component
let make = (~title, ~selected, ~color, ~onPress) => {
  let scale = ResAnimated.useSharedValue(0.0)

  React.useEffect1(_ => {
    open ResAnimated
    switch selected {
    | true =>
      scale.value = withSpring(
        ~toValue=1.0,
        ~userConfig=Spring.makeConfig(
          ~damping=15.0,
          ~mass=1.0,
          ~stiffness=250.0,
          ~overshootClamping=false,
          ~restSpeedThreshold=0.001,
          ~restDisplacementThreshold=0.001,
          (),
        ),
        (),
      )
    | false =>
      scale.value = withTiming(
        ~toValue=0.0,
        ~userConfig=Some(#Duration({Timing.duration: 250.0})),
        (),
      )
    }
    None
  }, [selected])

  let animatedStyle = {
    ResAnimated.useAnimatedStyle(() =>
      Style.style(~transform=[Style.scale(~scale=scale.value)], ~opacity=scale.value, ())
    )
  }

  <View style={styles["container"]}>
    <ResAnimated.View
      style={
        open ReactNative
        open Style
        array([styles["backer"], viewStyle(~backgroundColor=color, ()), animatedStyle])
      }
    />
    <Pressable style={_ => styles["presser"]} onPress={_ => onPress(!selected)}>
      {_ => <>
        <View style={styles["textContainer"]}> <Text> {title->React.string} </Text> </View>
      </>}
    </Pressable>
  </View>
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions