Skip to content

Commit

Permalink
fix(plasma-new-hope, plasma-core): fix types and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman233 committed Oct 27, 2023
1 parent 110d90e commit 644f194
Show file tree
Hide file tree
Showing 28 changed files with 88 additions and 100 deletions.
3 changes: 0 additions & 3 deletions packages/plasma-b2c/api/plasma-b2c.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ import { PopupBase } from '@salutejs/plasma-hope';
import { PopupBasePlacement } from '@salutejs/plasma-hope';
import { PopupBaseProps } from '@salutejs/plasma-hope';
import { PopupBaseProvider } from '@salutejs/plasma-hope';
import { PopupBaseRoot } from '@salutejs/plasma-hope';
import { popupBaseRootClass } from '@salutejs/plasma-hope';
import { PopupContextType } from '@salutejs/plasma-hope';
import { PopupInfo } from '@salutejs/plasma-hope';
Expand Down Expand Up @@ -688,8 +687,6 @@ export { PopupBaseProps }

export { PopupBaseProvider }

export { PopupBaseRoot }

export { popupBaseRootClass }

export { PopupContextType }
Expand Down
1 change: 0 additions & 1 deletion packages/plasma-b2c/src/components/PopupBase/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { PopupBaseProvider, usePopupBaseContext } from '@salutejs/plasma-hope';
export { PopupBase } from '@salutejs/plasma-hope';
export { usePopup } from '@salutejs/plasma-hope';
export { PopupBaseRoot } from '@salutejs/plasma-hope';
export { popupBaseRootClass, endAnimationClass, endTransitionClass } from '@salutejs/plasma-hope';

export type {
Expand Down
12 changes: 4 additions & 8 deletions packages/plasma-core/api/plasma-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FunctionComponent } from 'react';
import { HTMLAttributes } from 'react';
import { InterpolationFunction } from 'styled-components';
import { MutableRefObject } from 'react';
import { PropsWithChildren } from 'react';
import { default as React_2 } from 'react';
import { ReactNode } from 'react';
import { RefObject } from 'react';
Expand Down Expand Up @@ -879,7 +880,7 @@ export type PopupBasePlacement = BasicPopupBasePlacement | MixedPopupBasePlaceme
// @public (undocumented)
export interface PopupBaseProps extends React.HTMLAttributes<HTMLDivElement> {
children?: React.ReactNode;
frame?: 'document' | React.RefObject<HTMLElement>;
frame?: 'document' | string | React.RefObject<HTMLElement>;
isOpen: boolean;
// (undocumented)
offset?: [number, number] | [string, string];
Expand All @@ -892,12 +893,7 @@ export interface PopupBaseProps extends React.HTMLAttributes<HTMLDivElement> {
}

// @public (undocumented)
export const PopupBaseProvider: React_2.FC<{
children: ReactNode;
}>;

// @public
export const PopupBaseRoot: React_2.ForwardRefExoticComponent<PopupRootProps & React_2.RefAttributes<HTMLDivElement>>;
export const PopupBaseProvider: FC<PropsWithChildren>;

// @public (undocumented)
export const popupBaseRootClass = "popup-base-root";
Expand All @@ -917,7 +913,7 @@ export interface PopupInfo {
// (undocumented)
id: string;
// (undocumented)
info?: Object;
info?: Record<string, any>;
}

// @public (undocumented)
Expand Down
17 changes: 11 additions & 6 deletions packages/plasma-core/src/components/PopupBase/PopupBase.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState, forwardRef } from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';

Expand All @@ -15,13 +15,13 @@ const StyledPortal = styled.div``;
export const getClassName = (animationInfo: PopupAnimationInfo, className?: string) => {
const endAnimation = animationInfo?.endAnimation ? endAnimationClass : '';
const endTransition = animationInfo?.endTransition ? endTransitionClass : '';
return [className, endAnimation, endTransition].filter((item) => !!item).join(' ');
return [className, endAnimation, endTransition].filter(Boolean).join(' ');
};

/**
* Базовый копмонент PopupBase.
*/
export const PopupBase = React.forwardRef<HTMLDivElement, PopupBaseProps>(
export const PopupBase = forwardRef<HTMLDivElement, PopupBaseProps>(
(
{
id,
Expand Down Expand Up @@ -55,20 +55,25 @@ export const PopupBase = React.forwardRef<HTMLDivElement, PopupBaseProps>(
useEffect(() => {
let portal = document.getElementById(POPOVER_PORTAL_ID);

if (frame !== 'document' && frame && frame.current) {
if (typeof frame !== 'string' && frame && frame.current) {
portal = frame.current;
}

if (!portal) {
portal = document.createElement('div');
portal.setAttribute('id', POPOVER_PORTAL_ID);
document.body.appendChild(portal);

if (typeof frame === 'string' && frame !== 'document') {
document.getElementById(frame)?.appendChild(portal);
} else {
document.body.appendChild(portal);
}
}

portalRef.current = portal;

/**
* Изменение стейта нужно для того, чтобы PopupBase
* Изменение стейта нужно для того, чтобы Popup
* отобразился после записи DOM элемента в portalRef.current
*/
forceRender(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { ReactNode, useEffect } from 'react';
import React, { useEffect, useState, createContext, useContext, FC, PropsWithChildren } from 'react';

import { PopupContextType, PopupInfo } from './types';

export const POPOVER_PORTAL_ID = 'plasma-popup-root';

const items: PopupInfo[] = [];

const PopupBaseContext = React.createContext<PopupContextType>({
const PopupBaseContext = createContext<PopupContextType>({
items,
register(_info: PopupInfo): void {
throw new Error('Function not implemented. Add PopupBaseProvider');
Expand All @@ -16,15 +16,13 @@ const PopupBaseContext = React.createContext<PopupContextType>({
},
});

export const usePopupBaseContext = () => React.useContext(PopupBaseContext);
export const usePopupBaseContext = () => useContext(PopupBaseContext);

export const PopupBaseProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [items, setItems] = React.useState<PopupInfo[]>([]);
export const PopupBaseProvider: FC<PropsWithChildren> = ({ children }) => {
const [items, setItems] = useState<PopupInfo[]>([]);

const register = (info: PopupInfo) => {
const updatedItems = [...items];
updatedItems.push(info);
setItems(updatedItems);
setItems([...items, info]);
};

const unregister = (id: string) => {
Expand Down
12 changes: 6 additions & 6 deletions packages/plasma-core/src/components/PopupBase/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ export const usePopup = ({ isOpen, id, popupInfo, withAnimation }: PopupHookArgs

// для использования transition в качестве анимации
useEffect(() => {
if (withAnimation) {
animationInfo?.setEndTransition(animationInfo && (!isVisible || animationInfo?.endAnimation));
if (withAnimation && animationInfo) {
animationInfo.setEndTransition(!isVisible || animationInfo?.endAnimation);
}
}, [animationInfo?.endAnimation, isVisible]);
}, [animationInfo, withAnimation, isVisible]);

// сначала добавление/удаление из контекста, и только после этого отображение/скрытие
useEffect(() => {
// при первом открытии
if (isOpen && !isVisible) {
popupController.register({ id, ...popupInfo });
setVisible(true);
animationInfo?.setEndAnimation(false);
animationInfo.setEndAnimation(false);
return;
}

Expand All @@ -40,14 +40,14 @@ export const usePopup = ({ isOpen, id, popupInfo, withAnimation }: PopupHookArgs

// если есть анимация - закрытие по окончании анимации
if (withAnimation) {
animationInfo?.setEndAnimation(true);
animationInfo.setEndAnimation(true);
return;
}

// иначе обычное закрытие
popupController.unregister(id);
setVisible(false);
}, [isOpen, isVisible, animationInfo]);
}, [isOpen, isVisible, animationInfo, withAnimation]);

return { isVisible, setVisible, animationInfo };
};
1 change: 0 additions & 1 deletion packages/plasma-core/src/components/PopupBase/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { PopupBaseProvider, usePopupBaseContext } from './PopupBaseContext';
export { PopupBase } from './PopupBase';
export { usePopup } from './hooks';
export { PopupBaseRoot } from './PopupBaseRoot';
export { popupBaseRootClass, endAnimationClass, endTransitionClass } from './utils';

export type {
Expand Down
6 changes: 3 additions & 3 deletions packages/plasma-core/src/components/PopupBase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type PopupBasePlacement = BasicPopupBasePlacement | MixedPopupBasePlaceme

export interface PopupInfo {
id: string;
info?: Object;
info?: Record<string, any>;
}

export interface PopupContextType {
Expand All @@ -29,9 +29,9 @@ export interface PopupBaseProps extends React.HTMLAttributes<HTMLDivElement> {
*/
offset?: [number, number] | [string, string];
/**
* В каком контейнере позиционируется(по умолчанию document).
* В каком контейнере позиционируется(по умолчанию document), можно также указать id элемента или ref для него.
*/
frame?: 'document' | React.RefObject<HTMLElement>;
frame?: 'document' | string | React.RefObject<HTMLElement>;
/**
* Содержимое PopupBase.
*/
Expand Down
3 changes: 0 additions & 3 deletions packages/plasma-hope/api/plasma-hope.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ import { PopupBase } from '@salutejs/plasma-core';
import { PopupBasePlacement } from '@salutejs/plasma-core';
import { PopupBaseProps } from '@salutejs/plasma-core';
import { PopupBaseProvider } from '@salutejs/plasma-core';
import { PopupBaseRoot } from '@salutejs/plasma-core';
import { popupBaseRootClass } from '@salutejs/plasma-core';
import { PopupContextType } from '@salutejs/plasma-core';
import { PopupInfo } from '@salutejs/plasma-core';
Expand Down Expand Up @@ -1026,8 +1025,6 @@ export { PopupBaseProps }

export { PopupBaseProvider }

export { PopupBaseRoot }

export { popupBaseRootClass }

export { PopupContextType }
Expand Down
1 change: 0 additions & 1 deletion packages/plasma-hope/src/components/PopupBase/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { PopupBaseProvider, usePopupBaseContext } from '@salutejs/plasma-core';
export { PopupBase } from '@salutejs/plasma-core';
export { usePopup } from '@salutejs/plasma-core';
export { PopupBaseRoot } from '@salutejs/plasma-core';
export { popupBaseRootClass, endAnimationClass, endTransitionClass } from '@salutejs/plasma-core';

export type {
Expand Down
18 changes: 9 additions & 9 deletions packages/plasma-new-hope/src/components/Popup/Popup.styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { styled } from '@linaria/react';

import { PopupRootContainerProps } from './Popup.types';
import { DEFAULT_Z_INDEX } from './Popup.utils';
import type { PopupRootContainerProps } from './Popup.types';
import { DEFAULT_Z_INDEX } from './utils';

export const StyledPortal = styled.div``;

Expand All @@ -12,11 +12,11 @@ export const PopupView = styled.div`
`;

export const PopupRootContainer = styled.div<PopupRootContainerProps>`
position: ${(props) => (props?.frame === 'document' ? 'fixed' : 'absolute')};
z-index: ${(props) => props?.zIndex || DEFAULT_Z_INDEX};
left: ${(props) => props?.position.left || ''};
right: ${(props) => props?.position.right || ''};
top: ${(props) => props?.position.top || ''};
bottom: ${(props) => props?.position.bottom || ''};
transform: ${(props) => props?.position.transform || ''};
position: ${({ frame }) => (frame === 'document' ? 'fixed' : 'absolute')};
z-index: ${({ zIndex }) => zIndex || DEFAULT_Z_INDEX};
left: ${({ position }) => position.left || ''};
right: ${({ position }) => position.right || ''};
top: ${({ position }) => position.top || ''};
bottom: ${({ position }) => position.bottom || ''};
transform: ${({ position }) => position.transform || ''};
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
export const popupRootClass = 'popup-base-root';
export const endAnimationClass = 'popup-end-animation';
export const endTransitionClass = 'popup-end-transition';

export const DEFAULT_Z_INDEX = 9000;
18 changes: 10 additions & 8 deletions packages/plasma-new-hope/src/components/Popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import React, { forwardRef, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { useForkRef, useUniqId } from '@salutejs/plasma-core';
import { cx } from '@linaria/core';

import { RootProps } from '../../engines/types';
import { cx } from '../../utils';

import { BasicPopupPlacement, PopupPlacement, PopupPositionType, PopupProps } from './Popup.types';
import type { PopupPlacementBasic, PopupPlacement, PopupPositionType, PopupProps } from './Popup.types';
import { POPUP_PORTAL_ID } from './PopupContext';
import { PopupRoot } from './PopupRoot';
import { usePopup } from './Popup.hooks';
import { endAnimationClass, endTransitionClass } from './Popup.utils';
import { usePopup } from './hooks';
import { endAnimationClass, endTransitionClass } from './Popup.tokens';

export const handlePosition = (
placement: PopupPlacement,
offset: [number, number] | [string, string],
): PopupPositionType => {
let x = '0rem';
let y = '0rem';

if (offset) {
const [_x, _y] = offset;
x = typeof _x === 'number' ? `${_x}rem` : _x;
Expand All @@ -36,9 +37,9 @@ export const handlePosition = (
let top;
let bottom;
let transform;
const placements = placement.split('-') as BasicPopupPlacement[];
const placements = placement.split('-') as PopupPlacementBasic[];

placements.forEach((placement: BasicPopupPlacement) => {
placements.forEach((placement: PopupPlacementBasic) => {
switch (placement) {
case 'left':
left = x;
Expand Down Expand Up @@ -127,6 +128,7 @@ export const popupRoot = (Root: RootProps<HTMLDivElement, PopupProps>) =>
if (!portal) {
portal = document.createElement('div');
portal.setAttribute('id', POPUP_PORTAL_ID);

if (typeof frame === 'string' && frame !== 'document') {
document.getElementById(frame)?.appendChild(portal);
} else {
Expand All @@ -149,8 +151,8 @@ export const popupRoot = (Root: RootProps<HTMLDivElement, PopupProps>) =>

const cls = cx(
className,
animationInfo?.endAnimation && endAnimationClass,
animationInfo?.endTransition && endTransitionClass,
animationInfo?.endAnimation ? endAnimationClass : '',
animationInfo?.endTransition ? endTransitionClass : '',
);

return (
Expand Down
10 changes: 5 additions & 5 deletions packages/plasma-new-hope/src/components/Popup/Popup.types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type BasicPopupPlacement = 'center' | 'top' | 'bottom' | 'right' | 'left';
export type MixedPopupPlacement = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
export type PopupPlacement = BasicPopupPlacement | MixedPopupPlacement;
export type PopupPlacementBasic = 'center' | 'top' | 'bottom' | 'right' | 'left';
export type PopupPlacementMixed = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
export type PopupPlacement = PopupPlacementBasic | PopupPlacementMixed;

export interface PopupInfo {
id: string;
info?: Object;
info?: Record<string, any>;
}

export interface PopupContextType {
Expand All @@ -29,7 +29,7 @@ export interface PopupProps extends React.HTMLAttributes<HTMLDivElement> {
*/
offset?: [number, number] | [string, string];
/**
* В каком контейнере позиционируется(по умолчанию document).
* В каком контейнере позиционируется(по умолчанию document), можно также указать id элемента или ref для него.
*/
frame?: 'document' | string | React.RefObject<HTMLElement>;
/**
Expand Down
Loading

0 comments on commit 644f194

Please sign in to comment.