From 98f2db1e60f7757d755c1730852ff549c012cfdb Mon Sep 17 00:00:00 2001 From: Matthias Giger Date: Sun, 16 Oct 2022 21:03:05 +0200 Subject: [PATCH] feat(react): fixes and theme feature for native react implementation --- demo-ssr/package.json | 2 +- demo-ssr/pages/index.tsx | 12 ++++- package.json | 2 +- react.tsx | 98 ++++++++++++++++++++++++++++++---------- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/demo-ssr/package.json b/demo-ssr/package.json index 0d55fc2..525beff 100644 --- a/demo-ssr/package.json +++ b/demo-ssr/package.json @@ -22,7 +22,7 @@ "indicate": "file:.." }, "devDependencies": { - "@types/node": "18.8.3", + "@types/node": "18.8.5", "@types/react": "18.0.21", "@types/react-dom": "18.0.6", "eslint": "8.25.0", diff --git a/demo-ssr/pages/index.tsx b/demo-ssr/pages/index.tsx index 572ebea..babd6e0 100644 --- a/demo-ssr/pages/index.tsx +++ b/demo-ssr/pages/index.tsx @@ -226,7 +226,17 @@ const Home: NextPage = () => {
{``}
- + ({ backgroundColor: 'salmon' }), + hide: () => ({ backgroundColor: 'lime', opacity: 0.2 }), + }} + > diff --git a/package.json b/package.json index fef9ff8..54419fb 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ ], "devDependencies": { "@types/react": "^18.0.21", - "cypress": "^10.9.0", + "cypress": "^10.10.0", "padua": "^0.6.1" }, "prettier": "padua/configuration/.prettierrc.json", diff --git a/react.tsx b/react.tsx index 3716c91..b14d158 100644 --- a/react.tsx +++ b/react.tsx @@ -153,11 +153,7 @@ function Arrow({ icon, color, markup, image, direction }: ArrowProps & { directi ) } -const getArrowPosition = (arrow: false | Partial) => { - if (arrow === false) { - return 'normal' - } - +const getArrowPosition = (arrow: Partial) => { const position = arrow.position ?? 'center' if (position === 'center') { @@ -175,11 +171,18 @@ const getInline = (tag: ElementType, display?: string) => { return ['a', 'code', 'cite', 'span', 'strong', 'b', 'textarea'].includes(String(tag)) } -function Observers({ observersRef }: { observersRef: ObserverRefs }) { +function Observers({ + observersRef, + as: ObserverElementType = 'span', + theme, +}: Partial> & { + observersRef: ObserverRefs + as?: 'span' | 'tbody' +}) { return ( <> {directions.map((direction) => ( - ))} @@ -204,10 +208,12 @@ function Indicators({ handleIndicatorClick, width, color, -}: Partial & { + theme, +}: Partial> & { indicatorsRef: MutableRefObject visibility: Visibility handleIndicatorClick: (direction: Direction) => void + arrow: Partial }) { return ( <> @@ -229,6 +235,10 @@ function Indicators({ justifyContent: getArrowPosition(arrow), width: isHorizontal(direction) ? width : '100%', height: isVertical(direction) ? width : '100%', + ...theme.indicator, + ...(visibility[direction] + ? theme.show && theme.show(indicatorsRef[index]) + : theme.show && theme.hide(indicatorsRef[index])), }} role="button" aria-label={`Scroll ${direction}.`} @@ -266,7 +276,16 @@ interface Props { outerWrapperProps?: ReactHTMLDivElementProperties innerWrapperProps?: ReactHTMLDivElementProperties arrow?: boolean | Partial - theme?: any // TODO + theme?: { + outerWrapper?: CSSProperties + element?: CSSProperties + innerWrapper?: CSSProperties + indicator?: CSSProperties + observer?: CSSProperties + arrow?: CSSProperties + hide?: (indicator: HTMLSpanElement) => void | CSSProperties + show?: (indicator: HTMLSpanElement) => void | CSSProperties + } show?: (indicator: HTMLSpanElement) => void hide?: (hide: HTMLSpanElement) => void moveStylesToWrapper?: boolean @@ -300,6 +319,7 @@ export const Indicate = forwardRef(visibility) let content = null const handleIndicatorClick = useCallback((direction: Direction) => { const horizontalCurrent = isHorizontal(direction) @@ -335,6 +355,11 @@ export const Indicate = forwardRef { const visibilities = [] @@ -359,13 +384,13 @@ export const Indicate = forwardRef)?.current && options?.inlineStyles?.innerWrapper), @@ -420,15 +446,26 @@ export const Indicate = forwardRef {/* @ts-ignore */} {children} - +
) @@ -437,17 +474,28 @@ export const Indicate = forwardRef {createElement( as, { ref: innerWrapperRef, - style: { position: 'relative', verticalAlign: 'top', display: 'inline-flex' }, + style: { + position: 'relative', + verticalAlign: 'top', + display: 'inline-flex', + ...theme.innerWrapper, + }, }, <> {children} - + )} @@ -469,6 +517,7 @@ export const Indicate = forwardRef {children} - + ) } @@ -495,6 +545,7 @@ export const Indicate = forwardRef} indicatorsRef={indicatorsRef} handleIndicatorClick={handleIndicatorClick} width={width} color={color} + theme={theme} /> )