-
Notifications
You must be signed in to change notification settings - Fork 65
Add support for createRef
API
#172
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
Changes from 3 commits
73ebb39
a469d23
8b39bdc
59987d8
5678e34
81d138d
b703b8f
42c7274
76316db
15ad056
62106d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,9 @@ module React.DOM.Props where | |
|
||
import Prelude | ||
|
||
import Data.Nullable (Nullable) | ||
import Effect (Effect) | ||
import Effect.Uncurried (mkEffectFn1) | ||
import React (ReactRef) | ||
import React.Ref as Ref | ||
import React.SyntheticEvent | ||
( SyntheticEvent | ||
, SyntheticAnimationEvent | ||
|
@@ -20,6 +19,7 @@ import React.SyntheticEvent | |
, SyntheticUIEvent | ||
, SyntheticWheelEvent | ||
) | ||
import Web.HTML.HTMLElement (HTMLElement) | ||
|
||
foreign import data Props :: Type | ||
|
||
|
@@ -894,8 +894,8 @@ onScrollCapture f = unsafeMkProps "onScrollCapture" (mkEffectFn1 f) | |
onWheelCapture :: (SyntheticWheelEvent -> Effect Unit) -> Props | ||
onWheelCapture f = unsafeMkProps "onWheelCapture" (mkEffectFn1 f) | ||
|
||
ref :: (Nullable ReactRef -> Effect Unit) -> Props | ||
ref f = unsafeMkProps "ref" (mkEffectFn1 f) | ||
ref :: Ref.RefHandler HTMLElement -> Props | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I haven't used React Native myself, but I looked into this and as far as I can tell Native doesn't have refs - it seems to use something called Direct Manipulation instead. This does raise the question of why the React team put refs in the React library instead of ReactDOM; I'm not sure what the answer is. |
||
ref = unsafeMkProps "ref" | ||
|
||
suppressContentEditableWarning :: Boolean -> Props | ||
suppressContentEditableWarning = unsafeMkProps "suppressContentEditableWarning" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
|
||
exports.createRef = React.createRef; | ||
|
||
exports.getCurrentRef_ = function(ref) { | ||
if (ref.current) return ref.current; | ||
else return ref; | ||
elliotdavies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module React.Ref | ||
( Ref | ||
, RefHandler | ||
, ReactInstance | ||
, fromRef | ||
, fromEffect | ||
, getCurrentRef | ||
, createDOMRef | ||
, createInstanceRef | ||
) where | ||
|
||
import Prelude | ||
import Effect (Effect) | ||
import Data.Maybe (Maybe) | ||
import Web.HTML.HTMLElement (HTMLElement) | ||
import Data.Nullable (Nullable) | ||
import Data.Nullable as Nullable | ||
import Effect.Uncurried (EffectFn1, runEffectFn1, mkEffectFn1) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
|
||
foreign import data ReactInstance :: Type | ||
|
||
foreign import data Ref :: Type -> Type | ||
|
||
foreign import data RefHandler :: Type -> Type | ||
|
||
|
||
foreign import createRef :: forall a. Effect (Ref a) | ||
|
||
createDOMRef :: Effect (Ref HTMLElement) | ||
createDOMRef = createRef | ||
|
||
createInstanceRef :: Effect (Ref ReactInstance) | ||
createInstanceRef = createRef | ||
|
||
|
||
fromRef :: forall a. Ref a -> RefHandler a | ||
fromRef = unsafeCoerce | ||
|
||
|
||
fromEffect :: forall a. (Ref a -> Effect Unit) -> RefHandler a | ||
fromEffect = unsafeCoerce <<< mkEffectFn1 | ||
|
||
|
||
foreign import getCurrentRef_ :: forall a. EffectFn1 (Ref a) (Nullable a) | ||
|
||
getCurrentRef :: forall a. Ref a -> Effect (Maybe a) | ||
getCurrentRef ref = Nullable.toMaybe <$> runEffectFn1 getCurrentRef_ ref |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick note. This is coming from
purescript-web-html
, but I didn't notice the dependency in the bower file. Is it being pulled in from another dependency?On this note, I am unsure if we want this as a dependency since runtimes like react native, etc., would not necessarily require it. So far we have been attempting to keep dependencies at a minimum. That being said, I am not completely opposed to adding the dependency, but wanted to open it up for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ethul Well spotted; I think I forgot to push the change to the bower file. I had added
"purescript-web-html": "^2.2.0"
locally.I suppose that we could have a local
HTMLElement
type here and not use the library version. It would save on the dependency, but on the other hand anyone using DOM refs would have to write a conversion function (i.e.unsafeCoerce
), which might be annoying.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the dependency.