From a73402130efa829bee3d8c5a72b636ca445148bc Mon Sep 17 00:00:00 2001 From: mauroerta Date: Sun, 6 Jun 2021 17:47:24 +0200 Subject: [PATCH] feat(svelte): added support to default properties, docs and tests --- packages/svelte/README.md | 100 ++++++++++++++++++++++++++- packages/svelte/src/morfeoAction.ts | 19 ++++- packages/svelte/tests/morfeo.test.ts | 31 +++++++++ 3 files changed, 147 insertions(+), 3 deletions(-) diff --git a/packages/svelte/README.md b/packages/svelte/README.md index 4d472603..ef3ba285 100644 --- a/packages/svelte/README.md +++ b/packages/svelte/README.md @@ -1,11 +1,107 @@

@morfeo/svelte

-morfeo is a framework-agnostic set of tools that will help you to build your next design system based on a single source of truth: the theme. +**@morfeo/svelte** is part of the [morfeo](https://github.com/VLK-STUDIO/morfeo) eco-system: a framework-agnostic set of tools that will help you to build your next **design system** based on a single source of truth: the **theme**. + +---
Documentation | API | - Contributing | + Contributing | Slack
+ +--- + +**@morfeo/svelte** expose a [svelte action](https://svelte.dev/docs#use_action) that you can apply to your components to use morfeo. + +## Installation + +with [npm](https://www.npmjs.com/package/@morfeo/svelte): + +```bash +npm install @morfeo/web @morfeo/svelte +``` + +or [yarn](https://yarn.pm/@morfeo/svelte): + +```bash +yarn add @morfeo/web @morfeo/svelte +``` + +## Usage + +```html + + + +``` + +This component can now be stylized referring to the theme with inline style. + +```html + + +
+ +
+``` + +Or simply defining the style directly inside the theme object: + +```typescript +const myTheme = { + ...rest, + components: { + Button: { + style: { + bg: 'dark', + borderRadius: 'm', + px: 'xs', + py: 'xxs', + }, + props: { + type: 'submit' + }, + variants: { + primary: { + style: { + bg: 'primary', + borderWidth: 's', + borderColor: 'secondary', + }, + }, + submit: { + style: { + bg: 'secondary' + }, + props: { + type: 'submit' + }, + }, + }, + }, + }, +}; +``` + +```html + + +
+ + + + +
+``` + diff --git a/packages/svelte/src/morfeoAction.ts b/packages/svelte/src/morfeoAction.ts index 90ce0b21..ffc7552a 100644 --- a/packages/svelte/src/morfeoAction.ts +++ b/packages/svelte/src/morfeoAction.ts @@ -1,5 +1,5 @@ import { getStyles } from '@morfeo/jss'; -import { Style } from '@morfeo/web'; +import { Component, component, Style } from '@morfeo/web'; function getElementName({ componentName, variant }: Style) { if (componentName && variant) { @@ -9,14 +9,31 @@ function getElementName({ componentName, variant }: Style) { return componentName || 'morfeo-element'; } +function setAdditionalProps( + node: HTMLElement, + { componentName, variant }: Style, +) { + if (!componentName) { + return {}; + } + const props = component(componentName, variant).getProps(); + + if (!props) { + return; + } + Object.keys(props).forEach(prop => node.setAttribute(prop, props[prop])); +} + export function morfeo(node: HTMLElement, props: Style = {}) { const elementName = getElementName(props); let { classes, update, destroy } = getStyles({ [elementName]: props }); node.classList.add(classes[elementName]); + setAdditionalProps(node, props); return { update(nextProps: Style) { + setAdditionalProps(node, { ...props, ...nextProps }); update({ [elementName]: nextProps }); }, destroy, diff --git a/packages/svelte/tests/morfeo.test.ts b/packages/svelte/tests/morfeo.test.ts index f6371cc2..316290f4 100644 --- a/packages/svelte/tests/morfeo.test.ts +++ b/packages/svelte/tests/morfeo.test.ts @@ -11,6 +11,14 @@ const THEME: Theme = { style: { bg: 'secondary', }, + variants: { + span: { + style: {}, + props: { + 'data-test': 'additional prop', + }, + }, + }, }, }, } as any; @@ -61,4 +69,27 @@ describe('morfeo', () => { update({} as any); expect(typeof update).toBe('function'); }); + + test('should set the default properties to the element', () => { + const element = document.createElement('div'); + morfeo(element, { + componentName: 'Box', + variant: 'span', + }); + + expect(element.getAttribute('data-test')).toBe('additional prop'); + }); + + test('should update the default properties of to the element', () => { + const element = document.createElement('div'); + const { update } = morfeo(element, { + componentName: 'Box', + }); + + expect(element.getAttribute('data-test')).toBeFalsy(); + + update({ variant: 'span' }); + + expect(element.getAttribute('data-test')).toBe('additional prop'); + }); });