Skip to content

Commit

Permalink
recompile
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jun 28, 2024
1 parent 91cc593 commit 1e8b08f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import React, { createElement } from 'react';

import {
attachProps,
createForwardRef,
dashToPascalCase,
isCoveredByReact,
mergeRefs,
} from './utils';
import { attachProps, camelToDashCase, createForwardRef, dashToPascalCase, isCoveredByReact, mergeRefs } from './utils';

export interface HTMLStencilElement extends HTMLElement {
componentOnReady(): Promise<this>;
Expand All @@ -27,9 +21,9 @@ export const createReactComponent = <
ReactComponentContext?: React.Context<ContextStateType>,
manipulatePropsFunction?: (
originalProps: StencilReactInternalProps<ElementType>,
propsToPass: any,
propsToPass: any
) => ExpandedPropsTypes,
defineCustomElement?: () => void,
defineCustomElement?: () => void
) => {
if (defineCustomElement !== undefined) {
defineCustomElement();
Expand Down Expand Up @@ -58,17 +52,25 @@ export const createReactComponent = <
render() {
const { children, forwardedRef, style, className, ref, ...cProps } = this.props;

let propsToPass = Object.keys(cProps).reduce((acc, name) => {
let propsToPass = Object.keys(cProps).reduce((acc: any, name) => {
const value = (cProps as any)[name];

if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) {
const eventName = name.substring(2).toLowerCase();
if (typeof document !== 'undefined' && isCoveredByReact(eventName)) {
(acc as any)[name] = (cProps as any)[name];
acc[name] = value;
}
} else {
(acc as any)[name] = (cProps as any)[name];
// we should only render strings, booleans, and numbers as attrs in html.
// objects, functions, arrays etc get synced via properties on mount.
const type = typeof value;

if (type === 'string' || type === 'boolean' || type === 'number') {
acc[camelToDashCase(name)] = value;
}
}
return acc;
}, {});
}, {} as ExpandedPropsTypes);

if (manipulatePropsFunction) {
propsToPass = manipulatePropsFunction(this.props, propsToPass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';

import { OverlayEventDetail } from './interfaces';
import {
StencilReactForwardedRef,
attachProps,
dashToPascalCase,
defineCustomElement,
setRef,
} from './utils';
import { StencilReactForwardedRef, attachProps, dashToPascalCase, defineCustomElement, setRef } from './utils';

interface OverlayElement extends HTMLElement {
present: () => Promise<void>;
Expand All @@ -24,10 +18,7 @@ export interface ReactOverlayProps {
onWillPresent?: (event: CustomEvent<OverlayEventDetail>) => void;
}

export const createOverlayComponent = <
OverlayComponent extends object,
OverlayType extends OverlayElement
>(
export const createOverlayComponent = <OverlayComponent extends object, OverlayType extends OverlayElement>(
tagName: string,
controller: { create: (options: any) => Promise<OverlayType> },
customElement?: any
Expand Down Expand Up @@ -79,7 +70,7 @@ export const createOverlayComponent = <
if (this.props.onDidDismiss) {
this.props.onDidDismiss(event);
}
setRef(this.props.forwardedRef, null)
setRef(this.props.forwardedRef, null);
}

shouldComponentUpdate(nextProps: Props) {
Expand Down Expand Up @@ -113,25 +104,14 @@ export const createOverlayComponent = <
}

async present(prevProps?: Props) {
const {
children,
isOpen,
onDidDismiss,
onDidPresent,
onWillDismiss,
onWillPresent,
...cProps
} = this.props;
const { children, isOpen, onDidDismiss, onDidPresent, onWillDismiss, onWillPresent, ...cProps } = this.props;
const elementProps = {
...cProps,
ref: this.props.forwardedRef,
[didDismissEventName]: this.handleDismiss,
[didPresentEventName]: (e: CustomEvent) =>
this.props.onDidPresent && this.props.onDidPresent(e),
[willDismissEventName]: (e: CustomEvent) =>
this.props.onWillDismiss && this.props.onWillDismiss(e),
[willPresentEventName]: (e: CustomEvent) =>
this.props.onWillPresent && this.props.onWillPresent(e),
[didPresentEventName]: (e: CustomEvent) => this.props.onDidPresent && this.props.onDidPresent(e),
[willDismissEventName]: (e: CustomEvent) => this.props.onWillDismiss && this.props.onWillDismiss(e),
[willPresentEventName]: (e: CustomEvent) => this.props.onWillPresent && this.props.onWillPresent(e),
};

this.overlay = await controller.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ export const getClassName = (classList: DOMTokenList, newProps: any, oldProps: a
return finalClassNames.join(' ');
};

/**
* Transforms a React event name to a browser event name.
*/
export const transformReactEventName = (eventNameSuffix: string) => {
switch (eventNameSuffix) {
case 'doubleclick':
return 'dblclick';
}
return eventNameSuffix;
};

/**
* Checks if an event is supported in the current execution environment.
* @license Modernizr 3.0.0pre (Custom Build) | MIT
Expand All @@ -70,7 +81,7 @@ export const isCoveredByReact = (eventNameSuffix: string) => {
if (typeof document === 'undefined') {
return true;
} else {
const eventName = 'on' + eventNameSuffix;
const eventName = 'on' + transformReactEventName(eventNameSuffix);
let isSupported = eventName in document;

if (!isSupported) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export const dashToPascalCase = (str: string) =>
.split('-')
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
.join('');
export const camelToDashCase = (str: string) =>
str.replace(/([A-Z])/g, (m: string) => `-${m[0].toLowerCase()}`);
export const camelToDashCase = (str: string) => str.replace(/([A-Z])/g, (m: string) => `-${m[0].toLowerCase()}`);
27 changes: 10 additions & 17 deletions packages/react/src/components/react-component-lib/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ export type StencilReactForwardedRef<T> = ((instance: T | null) => void) | React

export const setRef = (ref: StencilReactForwardedRef<any> | React.Ref<any> | undefined, value: any) => {
if (typeof ref === 'function') {
ref(value)
ref(value);
} else if (ref != null) {
// Cast as a MutableRef so we can assign current
(ref as React.MutableRefObject<any>).current = value
(ref as React.MutableRefObject<any>).current = value;
}
};

export const mergeRefs = (
...refs: (StencilReactForwardedRef<any> | React.Ref<any> | undefined)[]
): React.RefCallback<any> => {
return (value: any) => {
refs.forEach(ref => {
setRef(ref, value)
})
}
refs.forEach((ref) => {
setRef(ref, value);
});
};
};

export const createForwardRef = <PropType, ElementType>(
ReactComponent: any,
displayName: string,
) => {
export const createForwardRef = <PropType, ElementType>(ReactComponent: any, displayName: string) => {
const forwardRef = (
props: StencilReactExternalProps<PropType, ElementType>,
ref: StencilReactForwardedRef<ElementType>,
ref: StencilReactForwardedRef<ElementType>
) => {
return <ReactComponent {...props} forwardedRef={ref} />;
};
Expand All @@ -44,14 +41,10 @@ export const createForwardRef = <PropType, ElementType>(
};

export const defineCustomElement = (tagName: string, customElement: any) => {
if (
customElement !== undefined &&
typeof customElements !== 'undefined' &&
!customElements.get(tagName)
) {
if (customElement !== undefined && typeof customElements !== 'undefined' && !customElements.get(tagName)) {
customElements.define(tagName, customElement);
}
}
};

export * from './attachProps';
export * from './case';
4 changes: 4 additions & 0 deletions packages/vue/src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const LaGutterItem = /*@__PURE__*/ defineContainer<JSX.LaGutterItem>('la-
export const LaTableOfContents = /*@__PURE__*/ defineContainer<JSX.LaTableOfContents>('la-table-of-contents', defineLaTableOfContents, [
'items',
'titleFilter',
'expanded',
'frbrExpressionUri',
'fetch',
'partner',
Expand All @@ -87,6 +88,9 @@ export const LaTableOfContentsController = /*@__PURE__*/ defineContainer<JSX.LaT
'titleFilterClearBtnClasses',
'expandAllBtnClasses',
'collapseAllBtnClasses',
'expandAllBtnText',
'collapseAllBtnText',
'expanded',
'titleFilterInputClasses',
'frbrExpressionUri',
'fetch',
Expand Down
Loading

0 comments on commit 1e8b08f

Please sign in to comment.