Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrow Next to Section Header #462

Open
Richard-Cao226 opened this issue Feb 2, 2023 · 1 comment
Open

Arrow Next to Section Header #462

Richard-Cao226 opened this issue Feb 2, 2023 · 1 comment

Comments

@Richard-Cao226
Copy link

Is there a way to add a dropdown arrow next to each section header? Something like below...

image

image

@blwinters
Copy link

I was able to achieve this with the following code. You can adjust the rotateZ amount according to your design.

export const CollapsibleSection = (props: CollapsibleSectionProps) => {
  const styles = useStyles()
  const [isCollapsed, setIsCollapsed] = useState(props.collapsedByDefault ?? false)

  const open = useSharedValue(isCollapsed)
  const progress = useDerivedValue(() => (open.value ? withTiming(1) : withTiming(0)))

  const onToggleCollapse = () => {
    setIsCollapsed(!isCollapsed)
    open.value = !open.value
  }

  return (
    <View>
      <RaisedBox style={styles.header} onPress={onToggleCollapse}>
        <Label color="$textMuted">{props.title}</Label>
        <Chevron progress={progress} />
      </RaisedBox>
      <Collapsible collapsed={isCollapsed}>
        {props.children}
      </Collapsible>
    </View>
  )
}

export interface ChevronProps {
  progress: Animated.SharedValue<number>
}

export const Chevron = ({ progress }: ChevronProps) => {
  const animatedStyle = useAnimatedStyle<AnimatedStyleProp<ViewStyle>>(() => {
    const value = Math.PI - progress.value * Math.PI
    return {
      transform: [{ rotateZ: `${value}rad` }],
    }
  })

  return (
    <Animated.View style={animatedStyle}>
      <Icon id="chevron-up" stroke="gray" />
    </Animated.View>
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants