Skip to content

Use createElement from @wordpress/element#184

Closed
gziolo wants to merge 1 commit intomasterfrom
try/create-element-mapper
Closed

Use createElement from @wordpress/element#184
gziolo wants to merge 1 commit intomasterfrom
try/create-element-mapper

Conversation

@gziolo
Copy link
Copy Markdown
Contributor

@gziolo gziolo commented Oct 23, 2018

This PR tries to refactor code to use createElement through @wordpres/element rather than directly to stay closer to how Gutenberg web works.

It's old PR which worked with Babel 6, we probably would have to rework it to make it up to date.

It also is part of my exploration of overriding createElement to allow usage of HTML tags as component names.

Requires WordPress/gutenberg#11129

/cc @Tug

@gziolo gziolo requested a review from hypest October 23, 2018 08:38
@gziolo gziolo added the [Type] Enhancement Improves a current area of the editor label Oct 23, 2018
@gziolo
Copy link
Copy Markdown
Contributor Author

gziolo commented Oct 23, 2018

I added Gutenberg part as a reference to how I tried to override createElement in WordPress/gutenberg#10934.

In a nutshell, I tried the following:

/**
 * External dependencies
 */
import { get, isString } from 'lodash';
import { createElement as createElementDefault } from 'react';
import {
	Circle,
	Defs,
	Ellipse,
	G,
	Line,
	Path,
	Polygon,
	TextPath,
	TSpan,
	Svg,
} from 'react-native-svg';

const htmlTagToNativeComponent = {
	circle: Circle,
	defs: Defs,
	ellipse: Ellipse,
	g: G,
	line: Line,
	path: Path,
	polygon: Polygon,
	svg: Svg,
	textPath: TextPath,
	tspan: TSpan,
	/*Polyline,
	Text,
	Rect,
	Use,
	Image,
	Symbol,
	LinearGradient,
	RadialGradient,
	Stop,
	ClipPath*/
};

const mapHTMLTagToNativeComponent = ( type ) => {
	if ( ! isString( type ) ) {
		return type;
	}

	return get( htmlTagToNativeComponent, [ type ], type );
};

/**
 * Returns a new element of given type. Type can be either a string tag name or
 * another function which itself returns an element.
 *
 * @param {?(string|Function)} type     Tag name or element creator.
 * @param {Object}             props    Element properties, either attribute
 *                                       set to apply to node or values to
 *                                       pass through to element creator.
 * @param {...WPElement}       children Descendant elements.
 *
 * @return {WPElement} Element.
 */
const createElement = ( type, props, ...children ) => {
	return createElementDefault(
		mapHTMLTagToNativeComponent( type ),
		props,
		...children
	);
};

export default createElement;

It didn't work properly.

@Tug Tug force-pushed the try/create-element-mapper branch from d03935d to c6f2f33 Compare October 24, 2018 15:03
@Tug Tug self-assigned this Oct 24, 2018
@Tug Tug force-pushed the try/create-element-mapper branch 5 times, most recently from 144f7f9 to bc632f1 Compare October 25, 2018 07:52
@gziolo
Copy link
Copy Markdown
Contributor Author

gziolo commented Oct 30, 2018

You made Flow and Jest unhappy, it probably needs more works for the configuration. However, this is exactly what should happen in terms of using Babel and consuming WordPress packages 👍

@Tug Tug mentioned this pull request Nov 19, 2018
@Tug Tug force-pushed the try/create-element-mapper branch from 6316975 to 2656bce Compare November 26, 2018 08:56
@Tug Tug force-pushed the try/create-element-mapper branch from 2656bce to fbaba3a Compare November 26, 2018 09:12
@gziolo
Copy link
Copy Markdown
Contributor Author

gziolo commented Dec 19, 2018

I don't think it is still valid. We can always reopen :)

@gziolo gziolo closed this Dec 19, 2018
@gziolo gziolo deleted the try/create-element-mapper branch December 19, 2018 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Enhancement Improves a current area of the editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants