,
) => ScaleProps[keyof ScaleProps]
+export type GetAllScalePropsFunction = () => ScaleProps
+
export interface ScaleConfig {
SCALES: DynamicScales
getScaleProps: GetScalePropsFunction
+ getAllScaleProps: GetAllScalePropsFunction
unit: string
}
@@ -74,6 +109,7 @@ const defaultDynamicLayoutPipe: DynamicLayoutPipe = scale1x => {
const defaultContext: ScaleConfig = {
getScaleProps: () => undefined,
+ getAllScaleProps: () => ({}),
SCALES: {
pl: defaultDynamicLayoutPipe,
pr: defaultDynamicLayoutPipe,
diff --git a/components/use-scale/utils.ts b/components/use-scale/utils.ts
index 2b0636ebd..ef5f8ba55 100644
--- a/components/use-scale/utils.ts
+++ b/components/use-scale/utils.ts
@@ -1,46 +1,39 @@
-import { ScaleProps } from './scale-context'
+import {
+ GetAllScalePropsFunction,
+ GetScalePropsFunction,
+ ScaleProps,
+ ScalePropKeys,
+} from './scale-context'
-export type ScalePropsAndInvalid = keyof ScaleProps | 'size'
+export const generateGetScaleProps = (
+ props: P & ScaleProps,
+): GetScalePropsFunction => {
+ const getScaleProps: GetScalePropsFunction = keyOrKeys => {
+ if (!Array.isArray(keyOrKeys)) return props[keyOrKeys as keyof ScaleProps]
+ let value = undefined
+ for (const key of keyOrKeys) {
+ const currentValue = props[key]
+ if (typeof currentValue !== 'undefined') {
+ value = currentValue
+ }
+ }
+ return value
+ }
+ return getScaleProps
+}
-export const ScalePropKeys: Array = [
- 'paddingLeft',
- 'pl',
- 'paddingRight',
- 'pr',
- 'paddingTop',
- 'pt',
- 'paddingBottom',
- 'pb',
- 'marginTop',
- 'mt',
- 'marginRight',
- 'mr',
- 'marginBottom',
- 'mb',
- 'marginLeft',
- 'ml',
- 'px',
- 'py',
- 'mx',
- 'my',
- 'width',
- 'height',
- 'font',
- 'unit',
- 'scale',
- 'size',
-]
-
-// export const withPureProps = >(
-// props: T,
-// ): Omit => {
-// if (!props) return {} as Omit
-// const keys = Object.keys(props).filter(key => key !== '')
-// const nextProps: any = {}
-// for (const key of keys) {
-// if (!(ScalePropKeys as string[]).includes(key)) {
-// nextProps[key] = props[key]
-// }
-// }
-// return nextProps
-// }
+export const generateGetAllScaleProps = (
+ props: P & ScaleProps,
+): GetAllScalePropsFunction => {
+ const getAllScaleProps: GetAllScalePropsFunction = () => {
+ let scaleProps: ScaleProps = {}
+ for (const key of ScalePropKeys) {
+ const value = props[key as keyof ScaleProps]
+ if (typeof value !== 'undefined') {
+ scaleProps[key as keyof ScaleProps] = value as any
+ }
+ }
+ return scaleProps
+ }
+ return getAllScaleProps
+}
diff --git a/components/use-scale/with-scale.tsx b/components/use-scale/with-scale.tsx
index fc4ad44d5..0d7ad21d2 100644
--- a/components/use-scale/with-scale.tsx
+++ b/components/use-scale/with-scale.tsx
@@ -1,13 +1,8 @@
import React, { forwardRef } from 'react'
-import {
- DynamicLayoutPipe,
- GetScalePropsFunction,
- ScaleConfig,
- ScaleContext,
- ScaleProps,
-} from './scale-context'
+import { DynamicLayoutPipe, ScaleConfig, ScaleContext, ScaleProps } from './scale-context'
import useTheme from '../use-theme'
import { isCSSNumberValue } from '../utils/collections'
+import { generateGetAllScaleProps, generateGetScaleProps } from './utils'
const reduceScaleCoefficient = (scale: number) => {
if (scale === 1) return scale
@@ -70,20 +65,9 @@ const withScale = (
const customFactor = factor * Number(attrValue)
return `calc(${customFactor} * ${unit})`
}
- const getScaleProps: GetScalePropsFunction = keyOrKeys => {
- if (!Array.isArray(keyOrKeys)) return props[keyOrKeys as keyof ScaleProps]
- let value = undefined
- for (const key of keyOrKeys) {
- const currentValue = props[key]
- if (typeof currentValue !== 'undefined') {
- value = currentValue
- }
- }
- return value
- }
const value: ScaleConfig = {
- unit,
+ unit: unit,
SCALES: {
pt: makeScaleHandler(paddingTop ?? pt ?? py ?? padding),
pr: makeScaleHandler(paddingRight ?? pr ?? px ?? padding),
@@ -101,7 +85,8 @@ const withScale = (
height: makeScaleHandler(height ?? h),
font: makeScaleHandler(font),
},
- getScaleProps,
+ getScaleProps: generateGetScaleProps(props),
+ getAllScaleProps: generateGetAllScaleProps(props),
}
return (