question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Pressable Component

See original GitHub issue

Would be nice if dripsy could export the Pressable component from react-native.

Sometimes the A component is not sufficient and one would like to use the Pressable. If i get it right, its basically a View component.

import { Pressable as RNPressable } from 'react-native'
import { createThemedComponent } from 'dripsy'

export const Pressable = createThemedComponent(RNPressable)

works fine for me.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nandorojocommented, Nov 18, 2020

Yeah, I agree. I was waiting for people to catch up on upgrading to RNW 0.14 before adding it in.

This is the code I’m using in my app now. Happy to add it to Dripsy along with #48

import React, { ReactNode, ComponentProps } from 'react'
import { styled } from 'dripsy'
import {
  Pressable as NativePressable,
  PressableStateCallbackType,
  ViewStyle,
} from 'react-native'

type CallbackWithHovered = PressableStateCallbackType & { hovered?: boolean }
const StyledPressable = styled(NativePressable)({})

type Props = {
  style?: ((prop: CallbackWithHovered) => ViewStyle) | ViewStyle
  children?: ReactNode | ((prop: CallbackWithHovered) => ReactNode)
} & Omit<ComponentProps<typeof StyledPressable>, 'children' | 'style'>

export default React.forwardRef(function Pressable(
  { sx = {}, ...props }: Props,
  ref: ComponentProps<typeof StyledPressable>['ref']
) {
  return (
    <StyledPressable
      {...props}
      ref={ref}
      sx={{
        cursor:
          props.onPress || props.accessibilityRole === 'link'
            ? 'pointer'
            : 'default',
        ...sx,
      }}
    />
  )
})

This supports this usage:

<Pressable>
  {({ hovered, pressable }) => ...}
</Pressable>

The one missing piece is adding the hovered, pressed methods as parts of the sx prop.

<Pressable sx={({ hovered, pressable }) => ({ bg: hovered ? 'primary' : 'text' })}>
  This isn't supported yet.
</Pressable>

I’ll have to give that some thought. Maybe if the sx prop is a function, we can pass it as a function to Pressable’s style, along with a css call. I’ll think about.

1reaction
nandorojocommented, Nov 18, 2020

React Native Web 0.14 extends Pressable to include hovered

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pressable - React Native
Pressable. Pressable is a Core Component wrapper that can detect various stages of press interactions on any of its defined children.
Read more >
How to add opacity to a Pressable component in React Native
In the wrapper component, add a new prop called activeOpacity . This prop accepts a number between 0 and 0.99 . It is...
Read more >
How To Use the New Pressable Component in React Native
reactnative #reactA quick overview of the new pressable component in React Native. Subscribe for React Native Videos: ...
Read more >
React Native - How to add opacity feedback to Pressable ...
The new Pressable component is great; I like being able to access new events but, how do you add the opacity feedback the...
Read more >
How to use Pressable Component in React Native - Morioh
Learn how to use Pressable Component in React Native. The Pressable is a new core component introduced in React Native version 0.63.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found