rescript-react-native icon indicating copy to clipboard operation
rescript-react-native copied to clipboard

hitSlop now support a single value instead of object

Open MoOx opened this issue 3 years ago • 6 comments

How would you like to have this implemented now it's supported on Android (it was only iOS before 0.69).

For now we have to use View.edgeInsets(?~top,?~bottom,?~left,?~right, ()) and I am sure a lot of people would like to do something like View.hitSlop(5). Maybe we could have hitSlop and hitSlopObj (to make this intuitive) ?

Feedback welcome !

MoOx avatar Jun 23 '22 10:06 MoOx

In View, we could simply add

// hitSlop prop accept number and edge inset object
external hitSlop: float => edgeInsets = ""
@obj
external hitSlopObj: (
  ~left: float=?,
  ~right: float=?,
  ~top: float=?,
  ~bottom: float=?,
  unit,
) => edgeInsets = ""

edgeInsets is used for other things than hitSlop, but having this shortcuts are cool imo.

No breaking change.

Any thoughts?

MoOx avatar Jun 23 '22 10:06 MoOx

poke @cknitt @Freddy03h

MoOx avatar Aug 10 '22 11:08 MoOx

hitSlopRect ? to be more close to React Object Type : https://reactnative.dev/docs/rect

Freddy03h avatar Aug 10 '22 12:08 Freddy03h

Good point, I think we should actually define the Rect object type, as it is used in multiple places. After #766 / with ReScript 10, we can do this as

type rect = {
  left?: float,
  right?: float,
  top?: float,
  bottom?: float,
}

Then we can have

type hitSlop

external hitSlop: float => hitSlop = ""
external hitSlopRect: rect => hitSlop = ""

cknitt avatar Aug 11 '22 00:08 cknitt

I didn't follow ReScript 10, this new record annotation mean automatic Option for ? attribute? I remember seing these for object but nor for record.

Freddy03h avatar Aug 11 '22 10:08 Freddy03h

It means that these are optional record fields, i.e., they can be omitted on record initialization and will also be omitted in the generated JS.

type rect = {
  left?: float,
  right?: float,
  top?: float,
  bottom?: float,
}

let myRect = { left: 5. }

will compile to

var myRect = {
  left: 5
};

cknitt avatar Aug 11 '22 12:08 cknitt

I am currently preparing this (to finally release 0.69). I am replacing View.edgeInsets (for now only deprecated) by a new rect type but I am wondering where I should put it. We don't have a "global" place to put reused things like this... It will be in View module. Unless you get a better idea guys ?

MoOx avatar Aug 18 '22 09:08 MoOx

Why not in types folder? In the RN doc it's just "Object Types"

Freddy03h avatar Aug 20 '22 22:08 Freddy03h

If I create ObjectTypes module, this will make us write <... hitSlop={ObjectTypes.hitSlop(5.)} ... or <... hitSlop={ObjectTypes.hitSlopRect({left: 5.})} .... I found that a bit verbose but at the same time I don't see a better idea since hitSlop is a props valid for View, Pressable & Touchables...

MoOx avatar Sep 09 '22 20:09 MoOx

I was thinking about a Rect module/file directly in the types folder, with the t object definition, which is enough with rescript 10.

hitSlop prop will accept a Rect.t

and refactor the View.edgeInsets to keep the same signature and return an Rect.t with a deprecated mention.

We don't need hitSlop or hitSlopRect functions.

Freddy03h avatar Sep 10 '22 16:09 Freddy03h

The idea for hitSlop / hitSlopRect is because hitSlop prop accepts both float (a single value for each direction) or a Rect.t. Is this information changing your mind ?

MoOx avatar Sep 10 '22 21:09 MoOx

oops, I forgot the initial idea ^^'

I wasn't against because we already used a function, but with rescript 10 it will be unnecessary so I think it will be better not to support the float. (like React.useState only support a function and not 'shortcut' with directly the value for example).

Looking at my code, I'm always using different values for horizontals and verticals values.

Freddy03h avatar Sep 11 '22 09:09 Freddy03h

So...

  • I have added a Rect.t type that will make more sense in next release, but used in...
  • View.edgeInsets (untouched for now), it will be deprecated in 0.70 with ReScript 10 breaking changes, because it will be replaced, in most place...
  • by HitSlop module, which contains t (similar but not the same as Rect.t) and a float which for now is a function, not an external (to avoid issue just in case somebody use HitSlop.float in a place where View.edgeInsets and is not an hitSlop...)

So for now: you can use HitSlop.float were View.edgeInsets. I won't communicate on this so people don't play with it for now.

In next release

  • most place using View.edgeInsets will need HitSlop.t (but not all, since not all are hitSlop).
  • Rect.t & HitSlop.t will become records with optional values, instead of [key]: option<float>
  • HitSlop.float will become a external

MoOx avatar Oct 04 '22 11:10 MoOx