Skip to content

Commit

Permalink
Handle namespaced jsx tagnames
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed Dec 15, 2023
1 parent 91fb9a2 commit 842fc0a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
7 changes: 6 additions & 1 deletion packages/noya-compiler/src/astBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ export function createExpressionCode(value: unknown): ts.Expression {

export function createElementCode({
name,
accessPath,
props,
children,
}: SimpleElement): ts.JsxElement | ts.JsxSelfClosingElement {
return createJsxElement(
ts.factory.createJsxOpeningElement(
ts.factory.createIdentifier(name),
accessPath && accessPath.length > 0
? // When we update typescript we can probably used JsxNamespacedName
// https://github.com/microsoft/TypeScript/blob/2c7162143bbbf567ccecc64105010699fa7a2128/src/compiler/factory/nodeFactory.ts#L5802
ts.factory.createIdentifier(`${name}.${accessPath[0]}`)
: ts.factory.createIdentifier(name),
undefined,
ts.factory.createJsxAttributes(
Object.entries(props).flatMap(([key, value]) => {
Expand Down
1 change: 1 addition & 0 deletions packages/noya-compiler/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Passthrough = { [passthroughSymbol]: true };
export type SimpleElement = {
[simpleElementSymbol]: true;
name: string;
accessPath?: string[];
source?: string;
props: Record<string, unknown>;
children: (SimpleElement | string | Passthrough)[];
Expand Down
14 changes: 8 additions & 6 deletions packages/noya-compiler/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ type ResolvedCompilerConfiguration = CompilerConfiguration & {
function findElementNameAndSource(
element: React.ReactNode,
DesignSystem: DesignSystemDefinition,
Components: Map<unknown, NamespaceItem>,
namespaceMap: Map<unknown, NamespaceItem>,
):
| {
name: string;
element: React.ReactElement;
source?: string;
accessPath?: string[];
}
| undefined {
if (!React.isValidElement(element)) return;
Expand All @@ -64,10 +65,10 @@ function findElementNameAndSource(
}

// This is a component exported directly from the design system
const component = Components.get(element.type);
const namespaceItem = namespaceMap.get(element.type);

if (component) {
return { ...component, element };
if (namespaceItem) {
return { ...namespaceItem, element };
}

const protocolComponent = Object.values(DesignSystem.components).find(
Expand All @@ -79,7 +80,7 @@ function findElementNameAndSource(

if (!isValidElement(libraryElement)) return;

return findElementNameAndSource(libraryElement, DesignSystem, Components);
return findElementNameAndSource(libraryElement, DesignSystem, namespaceMap);
}

export function createSimpleElement(
Expand All @@ -96,7 +97,7 @@ export function createSimpleElement(

if (!elementType) return;

const { element, name, source } = elementType;
const { element, name, source, accessPath } = elementType;

function toReactArray(children: ReactNode): ReactNode[] {
const result: ReactNode[] = [];
Expand Down Expand Up @@ -137,6 +138,7 @@ export function createSimpleElement(

return simpleElement({
name,
accessPath,
source,
// Filter out children prop and undefined props
props: Object.fromEntries(
Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/dseditor/DSEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ function DSGalleryCode({
definitions: [
'vanilla',
'@noya-design-system/chakra',
// '@noya-design-system/antd',
'@noya-design-system/antd',
'@noya-design-system/mui',
],
});
Expand Down

0 comments on commit 842fc0a

Please sign in to comment.