Skip to content

Commit

Permalink
feat: class used in morfeo component
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Oct 18, 2021
1 parent e9fbd4b commit 38bac81
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, component } from '@morfeo/react';
import { useMemo } from 'react';
import { useRouter } from '../../../../hooks';
import { Grid, Item } from '../../../Grid';
import { createMorfeoComponent } from '../createMorfeoComponent';
import { MorfeoComponent } from '../MorfeoComponent';
import { Preview } from '../Preview';

import styles from './style.module.css';
Expand All @@ -16,12 +16,11 @@ export const Detail = () => {
const variantKeys = Object.keys(variants || {});

const componentPreview = useMemo(() => {
return createMorfeoComponent({
name: componentName as Component,
props: {},
variant,
children: [componentName],
});
return (
<MorfeoComponent name={componentName as Component} variant={variant}>
{componentName}
</MorfeoComponent>
);
}, [componentName, variant]);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { component, Component, useClassName } from '@morfeo/react';
import clsx from 'clsx';

type Props = {
name: Component;
variant?: string;
};

export const MorfeoComponent: React.FC<Props> = ({
name,
variant,
children,
...props
}) => {
const { tag, props: componentProps = {} } = component(name, variant).get();
const className = useClassName({ componentName: name, variant });

return React.createElement((tag || 'div') as any, {
...componentProps,
...props,
className: clsx(
className,
(props as any).className,
(componentProps as any).className,
),
style: {
...(props as any).style,
...(componentProps as any).style,
},
children,
});
};
6 changes: 4 additions & 2 deletions devtool/src/_shared/components/Slices/Components/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRouter } from '../../../hooks/useRouter';
import { RouteName } from '../../../contexts';
import { SliceName } from '../../../contexts/Routing/types';
import { Card } from '../../Card';
import { createMorfeoComponent } from './createMorfeoComponent';
import { MorfeoComponent } from './MorfeoComponent';
import clsx from 'clsx';

import styles from './style.module.css';
Expand Down Expand Up @@ -63,7 +63,9 @@ export const Preview: React.FC<Props> = ({ name, variant }) => {
className={styles.previewContainer}
copyText={variant || name}
>
{createMorfeoComponent({ name, variant, props: {}, children: [name] })}
<MorfeoComponent name={name} variant={variant}>
{name}
</MorfeoComponent>
</Card>
<Info name={name} variant={variant} />
</Card>
Expand Down

This file was deleted.

0 comments on commit 38bac81

Please sign in to comment.