Skip to content

Commit

Permalink
feat: update quick and srr versions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobslisboa committed Sep 3, 2024
1 parent 700d58c commit e0631a2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 38 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ install:
opam install . --deps-only --with-test
cd demo && yarn install

.PHONY: pin
pin: ## Pin dependencies
@opam pin add -y quickjs "https://github.com/ml-in-barcelona/quickjs.ml.git#0.1.1"

.PHONY: init
init: create-switch pin install ## Create a local dev enviroment

Expand Down
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
melange
melange-webapi
dream
quickjs
(quickjs (>= 0.1.2))
(reason-react-ppx
(>= 0.14.0))
(reason-react
Expand All @@ -44,7 +44,7 @@
(= 0.26.1)
:with-test))
(server-reason-react
(= 0.2.0))
(= 0.3.1))
(melange-testing-library :with-test)
(melange-jest :with-test)
(ocaml-lsp-server :with-test)))
10 changes: 10 additions & 0 deletions packages/modal/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(library
(name shared_js)
(public_name universal-portal.modal.js)
(modes melange)
(wrapped false)
(libraries universal-portal.shared_js reason-react melange-webapi)
(preprocess
(pps melange.ppx reason-react-ppx server-reason-react.browser_ppx -js)))

(copy_files# "../native/*.re")
30 changes: 30 additions & 0 deletions packages/modal/native/UniversalModal.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[@react.component]
let make = () => {
<UniversalPortal_Shared.Portal selector="body">
<div
style={ReactDOM.Style.make(
~position="absolute",
~top="0",
~left="0",
~width="100%",
~backgroundColor="rgba(0, 0, 0, 0.3)",
~height="100%",
~display="flex",
~justifyContent="center",
~alignItems="center",
(),
)}>
<div
style={ReactDOM.Style.make(
~backgroundColor="white",
~padding="30px",
~width="200px",
~height="200px",
(),
)}>
{"Hey, I'm a portal, disable JS on your dev tools and check that I'll still here"
|> React.string}
</div>
</div>
</UniversalPortal_Shared.Portal>;
};
16 changes: 16 additions & 0 deletions packages/modal/native/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(library
(name shared_native)
(public_name universal-portal.modal.native)
(modules :standard)
(libraries
universal-portal.shared_native
server-reason-react.react
server-reason-react.reactDom)
(wrapped false)
(preprocess
(pps
server-reason-react.ppx
server-reason-react.browser_ppx
server-reason-react.melange_ppx)))

(copy_files# "../*.re")
18 changes: 18 additions & 0 deletions packages/server/src/UniversalPortal_Server.re
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ let collectPortals = (element, callback) => {
element
</PortalCollector>;
};

let withPortals = element => {
let portals: ref(array(UniversalPortal_Shared.Portal.portal)) = ref([||]);

let element =
ReactDOM.renderToString(
collectPortals(
element, (collectedPortal: UniversalPortal_Shared.Portal.portal) => {
portals := Array.append(portals^, [|collectedPortal|])
}),
);

let html = appendUniversalPortals(element, portals^);

portals := [||];

html;
};
66 changes: 36 additions & 30 deletions packages/shared/native/UniversalPortal_Shared_Portal.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@ type portal = {
};
let portalCollectorContext = React.createContext((_: portal) => ());

[@react.component]
let make = (~children, ~selector) => {
let context = React.useContext(portalCollectorContext);
let (portalNode, setPortalNode) = React.useState(() => None);

let createPortal = domElement =>
ReactDOM.createPortal(children, domElement);

React.useEffect1(
() => {
let domElement = ReactDOM.querySelector(selector);
setPortalNode(_ => domElement);

None;
},
[||],
);

switch%platform (Runtime.platform) {
| Client =>
// Portal doesn't need to be collected on the client
context |> ignore
| Server => context({selector, content: children})
};

switch (portalNode) {
| Some(node) => createPortal(node)
| _ => React.null
};
};
// Js component that renders a portal
[@platform js]
include {
[@react.component]
let make = (~children, ~selector) => {
let (portalNode, setPortalNode) = React.useState(() => None);

let createPortal = domElement =>
ReactDOM.createPortal(children, domElement);

React.useEffect(() => {
let domElement = ReactDOM.querySelector(selector);
setPortalNode(_ => domElement);

None;
});

switch (portalNode) {
| Some(node) => createPortal(node)
| _ => React.null
};
};
};

// Native component that collects the portals
[@platform native]
include {
[@react.component]
let make = (~children, ~selector) => {
let context = React.useContext(portalCollectorContext);

context({selector, content: children});

React.null;
};
};
4 changes: 2 additions & 2 deletions universal-portal.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ depends: [
"melange"
"melange-webapi"
"dream"
"quickjs"
"quickjs" {>= "0.1.2"}
"reason-react-ppx" {>= "0.14.0"}
"reason-react" {>= "0.14.0"}
"lambdasoup"
"alcotest" {with-test}
"alcotest-lwt" {with-test}
"fmt" {with-test}
"ocamlformat" {= "0.26.1" & with-test}
"server-reason-react" {= "0.2.0"}
"server-reason-react" {= "0.3.1"}
"melange-testing-library" {with-test}
"melange-jest" {with-test}
"ocaml-lsp-server" {with-test}
Expand Down

0 comments on commit e0631a2

Please sign in to comment.