Skip to content

Commit

Permalink
feat(plasma-new-hope): resolve circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanKuzmich committed Dec 22, 2024
1 parent 6ad8287 commit 40612bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
40 changes: 0 additions & 40 deletions packages/plasma-new-hope/src/engines/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,13 @@ import React from 'react';
import { _component } from './linaria';
import type {
ComponentConfig,
HTMLAnyAttributes,
HTMLTagList,
PropsType,
Variants,
HTMLAttributesWithoutOnChange,
HTMLAttributesWithoutOnChangeAndDefaultValue,
} from './types';

//
// Тип HTMLAttributesOmitOnChange требуется чтобы использовать компонент с кастомным пропсом onChange
//

export const getStaticVariants = (config: ComponentConfig) => {
if (!config.variations) {
return [];
}
const res = [];
const { variations } = config;

// eslint-disable-next-line guard-for-in
for (const key in variations) {
const { css } = variations[key];
css && res.push(css);
}

return res;
};

export const getDynamicVariants = (config: ComponentConfig) => {
return (props: HTMLAnyAttributes) => {
const res = [];

// eslint-disable-next-line guard-for-in
for (const key in config.variations) {
if (key in props) {
const variant = config.variations[key];

const css = variant[props[key]];
// no css for { modifier: true }
css && Array.isArray(css) ? res.push(...css) : res.push(css);
}
}

return res;
};
};

export const mergeConfig = <
Tag extends HTMLTagList,
VariantList extends Variants,
Expand Down
2 changes: 1 addition & 1 deletion packages/plasma-new-hope/src/engines/emotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { forwardRef } from 'react';
import { css, SerializedStyles } from '@emotion/react';
import styled from '@emotion/styled';

import { getStaticVariants, getDynamicVariants } from './common';
import { getStaticVariants, getDynamicVariants } from './utils';
import type { ComponentConfig, HTMLAnyAttributes } from './types';

export { css };
Expand Down
2 changes: 1 addition & 1 deletion packages/plasma-new-hope/src/engines/linaria.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cx } from '@linaria/core';

// TODO: #1008 Избавиться от импортов и переделать addFocus
import 'focus-visible';
import { getStaticVariants, getDynamicVariants } from './common';
import { getStaticVariants, getDynamicVariants } from './utils';
import type { ComponentConfig, HTMLAnyAttributes } from './types';

/* eslint-disable no-underscore-dangle */
Expand Down
2 changes: 1 addition & 1 deletion packages/plasma-new-hope/src/engines/styled-components.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { forwardRef } from 'react';
import styled, { css } from 'styled-components';

import { getStaticVariants, getDynamicVariants } from './common';
import { getStaticVariants, getDynamicVariants } from './utils';
import type { ComponentConfig, HTMLAnyAttributes } from './types';

export { styled, css };
Expand Down
36 changes: 36 additions & 0 deletions packages/plasma-new-hope/src/engines/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { ComponentConfig, HTMLAnyAttributes } from './types';

export const getStaticVariants = (config: ComponentConfig) => {
if (!config.variations) {
return [];
}
const res = [];
const { variations } = config;

// eslint-disable-next-line guard-for-in
for (const key in variations) {
const { css } = variations[key];
css && res.push(css);
}

return res;
};

export const getDynamicVariants = (config: ComponentConfig) => {
return (props: HTMLAnyAttributes) => {
const res = [];

// eslint-disable-next-line guard-for-in
for (const key in config.variations) {
if (key in props) {
const variant = config.variations[key];

const css = variant[props[key]];
// no css for { modifier: true }
css && Array.isArray(css) ? res.push(...css) : res.push(css);
}
}

return res;
};
};

0 comments on commit 40612bf

Please sign in to comment.