Skip to content

Feat: Atomic Render Nodes #5

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

Merged
merged 30 commits into from
Oct 7, 2022
Merged

Feat: Atomic Render Nodes #5

merged 30 commits into from
Oct 7, 2022

Conversation

barelyhuman
Copy link
Owner

An attempt at getting rid of the dependency on React for building the tree

@barelyhuman
Copy link
Owner Author

change of plan, we're building a Mutable DOM for base set of nodes for ReactNative first

@barelyhuman
Copy link
Owner Author

This is now valid code

import {Document, render} from './src/lib/dom';

function App() {
  const document = new Document();
  const textElm = document.createElement('Text');

  let count = 0;

  const text = document.createTextNode(count);
  const safeAreaView = document.createElement('SafeAreaView');

  textElm.appendChild(text);

  setInterval(() => {
    text.data = ++count;
  }, 1000);

  safeAreaView.appendChild(textElm);
  const tree = render(safeAreaView);

  return tree;
}

export default App;

also standardises how to work with the bridge and dom nodes
still need to add compat with web DOM to support maximum
frameworks
@barelyhuman barelyhuman linked an issue Sep 30, 2022 that may be closed by this pull request
@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 30, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4bbae96:

Sandbox Source
Vanilla Configuration

add handlers for prop updation
this includes updates to the style props and proxies the event
addition to props for press functionality.

the commit also adds all the
remaining non-deprecated
components to the registry
@barelyhuman
Copy link
Owner Author

Close to a full DOM
the above commits add the ability to do the following

  • add Press event handlers (proxies the addition to the host component's props)
  • add style updates via Object.assign or using style[property] format on the node
  • add prop updates for any other attributes that might be added

Example of the above changes

import {Document, render} from './src/lib/dom';

const document = new Document();

function App() {
  let count = 0;
  const safeAreaView = document.createElement('SafeAreaView');
  const countTextNode = document.createTextNode(count);
  const countText = document.createElement('Text');
  countText.appendChild(countTextNode);

  // set the isHighlighted prop on the react native host component Text
  countText.setAttribute('isHighlighted', true);

  safeAreaView.style.marginTop = 100;

  Object.assign(countText.style, {
    fontSize: 52,
    textAlign: 'center',
  });

  const buttonNode = createButton();

  buttonNode.addEventListener('onPress', () => {
    const childText = buttonNode.children[0];
    const buttonTNode = childText.children[0];
    countTextNode.data = ++count;
    const isEven = count % 2 === 0;
    buttonTNode.data = isEven ? 'Even' : 'Odd';
    Object.assign(childText.style, {
      color: count % 2 === 0 ? 'green' : 'red',
    });
  });

  safeAreaView.appendChild(countText);
  safeAreaView.appendChild(buttonNode);

  let tree = render(safeAreaView);
  return tree;
}

function createButton() {
  const touchable = document.createElement('TouchableOpacity');
  const text = document.createElement('Text');
  const textNode = document.createTextNode('Press Me');

  Object.assign(touchable.style, {
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: 6,
    margin: 12,
  });

  Object.assign(text.style, {
    color: 'black',
    fontSize: 45,
  });

  text.appendChild(textNode);
  touchable.appendChild(text);
  return touchable;
}

export default App;

@barelyhuman
Copy link
Owner Author

Checkpoint:
Preact can render and update text nodes and native view components to the DOM

Merging.

@barelyhuman barelyhuman merged commit 492bdc2 into main Oct 7, 2022
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 this pull request may close these issues.

Mutable Web Like Node Tree , aka DOM
1 participant