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

Support react-native (view-only) #363

Open
Shaddix opened this issue Jan 10, 2022 · 9 comments · May be fixed by #368
Open

Support react-native (view-only) #363

Shaddix opened this issue Jan 10, 2022 · 9 comments · May be fixed by #368
Assignees

Comments

@Shaddix
Copy link
Contributor

Shaddix commented Jan 10, 2022

It'd be great to support react-native!

Use case: web-based react-native designer, render the results (view-only) in react-native.

View-only mode (<Editor enabled={false}>) doesn't actually depend on anything dom-related, so it seems easy to make it work under react-native.

I tried it as a proof-of-concept, and it seems that there are only 2 things that needs to be changed:

  1. getRandomId should use nanoid/non-secure ('nanoid' doesn't work in react-native)
  2. RenderIndicator should import react-dom conditionally. Something like
const isReactNative =
  typeof window !== 'undefined' &&
  window.navigator &&
  window.navigator.product &&
  window.navigator.product === 'ReactNative';

const ReactDOM = !isReactNative ? require('react-dom') : null;

And that's it! After only these two changes it works on React-Native!

I could provide a PR if you are ok with the idea :)

Shaddix added a commit to Shaddix/craft.js that referenced this issue Jan 11, 2022
@Shaddix
Copy link
Contributor Author

Shaddix commented Jan 29, 2022

seems like the 2nd point is not actually needed, if react-dom is shimmed in react-native project.

@Shaddix
Copy link
Contributor Author

Shaddix commented Jan 29, 2022

here's an example repo with react-native project: https://github.com/Shaddix/craft_js_native
(though, it only works if node_modules/@craftjs/utils/dist folder is replaced with compiled version from PR branch (i.e. if it uses nanoid/non-secure)

@Shaddix
Copy link
Contributor Author

Shaddix commented Jan 29, 2022

aforementioned repo contains JSON 'markup': https://github.com/Shaddix/craft_js_native/blob/master/craft/content.json which is rendered via craftjs in react-native.

If you wish, I could come up with a better looking example and add it to examples folder of this repo

@ggunti
Copy link

ggunti commented Mar 25, 2022

Actually I started to build a web-based react-native designer based on craftjs.
It includes a web app (the UI designer) + android & ios mobile app (view-only). It is at the beginning but you can add it as example if you want.
https://github.com/ggunti/rnw-ui-builder

@Shaddix
Copy link
Contributor Author

Shaddix commented Mar 25, 2022

@ggunti that's quite interesting!
Though, as I see you are using CraftJS on the Editor side, and then generate a regular react-native code to be added to a project.

My idea was a bit different, I wanted to actually use the same component within React-Native app, providing it with the same config that was generated on the web. E.g. to be able to use within react-native something like this (taken from Readme):

const App = () => {
  const jsonString = /* retrieve JSON from server */
  return (
    <Editor>
      <Frame json={jsonString}>
        ...
      </Frame>
    </Editor>
  )
}

My final goal is very similar to what you are doing - to provide a web UI for designing react-native screens.
But at the same time I wanted to have an ability to change UI within Admin panel, and have apps update themselves automatically by re-downloading the changed configs (without the need for developers to recompile and publish the apps).

And actually usage of <Editor><Frame> within react-native is possible with a single change to the CraftJS library (i.e. using 'nanoid/non-secure' instead of 'nanoid' to generate ids).

While PR is not accepted, I plan to actually use a patch-package to apply this change and move on.

@ggunti
Copy link

ggunti commented Mar 25, 2022

@Shaddix
I also allow to build the UI on web and use the same component withing React Native app. You can see what you built directly in the mobile apps, no recompilation needed. Generating code is just an additional feature in case that a developer wants to export the UI and include it into own app, which could speed up his development process.

Btw, I did not face the 'nanoid' problem, this is all what I did and craftjs preview worked also on react-native:

if (Platform.OS === 'android' || Platform.OS === 'ios') {
  // @ts-ignore
  document = {
    querySelector: () => {},
  };
}

@Shaddix
Copy link
Contributor Author

Shaddix commented Mar 25, 2022

nice, I haven't found this feature while quickly going through your website, probably missed it.

I'm gonna try again with latest version, maybe it's gonna work, thanks for the advice!

@franz-fletcher
Copy link

Highly recommend looking into react-native-web for your native preview needs. It's a pretty healthy renderer so it most likely has viable solutions to make the task more of an integration and less of a feature implementation task for craft.js support.

@aqos156
Copy link

aqos156 commented Oct 24, 2023

Any info on this? We are trying to run this in expo on web for editing, didn't get yet to native side, because we are having trouble creating the editor ui within react native web. The drag and drop works, but indicator is missing, it is quite slow compared to the examples on web and we often get the following error

Uncaught TypeError: Cannot read properties of null (reading 'getBoundingClientRect')
    at de (index.js:1:1)
    at e.value (index.js:1:1)
    at index.js:1:1
    at HTMLDivElement.i (index.js:1:1)

any ideas? A working example repository? Thank you in advance!

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

Successfully merging a pull request may close this issue.

4 participants