-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: gradients parsers added to @morfeo/web package
- Loading branch information
Showing
12 changed files
with
334 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const gradientsMap = ['gradient', 'bgGradient', 'textGradient'] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { gradientsMap } from '../properties'; | ||
import { Color } from './colors'; | ||
|
||
export type GradientCoordinate = { | ||
x: number; | ||
y: number; | ||
}; | ||
|
||
export type GradientKind = 'linear' | 'radial'; | ||
|
||
export interface ReactNativeGradientConfig { | ||
end: GradientCoordinate; | ||
start: GradientCoordinate; | ||
colors: Color[]; | ||
locations?: number[]; | ||
angle?: number; | ||
useAngle?: boolean; | ||
angleCenter?: GradientCoordinate; | ||
} | ||
|
||
export interface GradientConfig { | ||
end?: number; | ||
kind?: GradientKind; | ||
start?: number; | ||
angle?: number; | ||
colors: Color[]; | ||
} | ||
|
||
export interface Gradients { | ||
primary: GradientConfig; | ||
secondary: GradientConfig; | ||
} | ||
|
||
export type Gradient = keyof Gradients; | ||
|
||
type BaseGradientProps = { | ||
[K in typeof gradientsMap[number]]: Gradient; | ||
}; | ||
|
||
export interface GradientProps extends BaseGradientProps {} | ||
|
||
export type GradientProperty = keyof GradientProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
theme, | ||
Gradient, | ||
ParserParams, | ||
SliceParsers, | ||
GradientConfig, | ||
gradientProperties, | ||
GradientProperty, | ||
} from '@morfeo/core'; | ||
|
||
type GradientsParsers = SliceParsers< | ||
typeof gradientProperties, | ||
keyof typeof gradientProperties | ||
>; | ||
|
||
function getGradientPercentages({ | ||
start = 0, | ||
end = 100, | ||
colors = [], | ||
}: GradientConfig) { | ||
const { length } = colors; | ||
const diff = end - start; | ||
const part = diff / (length - 1 > 0 ? length - 1 : 1); | ||
let percentage = 0; | ||
|
||
return colors.reduce((prev, colorKey) => { | ||
const color = theme.getValue('colors', colorKey) || colorKey; | ||
const current = `${color} ${start + percentage}%`; | ||
percentage += part; | ||
return prev ? `${prev}, ${current}` : current; | ||
}, ''); | ||
} | ||
|
||
function getGradientProperty({ kind }: GradientConfig) { | ||
if (kind === 'radial') { | ||
return 'radial-gradient'; | ||
} | ||
|
||
return 'linear-gradient'; | ||
} | ||
|
||
function getGradientBackground(value: Gradient) { | ||
const config = theme.getValue('gradients', value) || {}; | ||
const { angle = 180, kind } = config; | ||
const property = getGradientProperty(config); | ||
const percentages = getGradientPercentages(config); | ||
const gradientAngle = kind === 'radial' ? 'circle' : `${angle}deg`; | ||
|
||
if (!percentages) { | ||
return undefined; | ||
} | ||
|
||
return `${property}(${gradientAngle}, ${percentages})`; | ||
} | ||
|
||
function gradient({ value }: ParserParams<GradientProperty>) { | ||
const bg = getGradientBackground(value as any); | ||
return { | ||
background: bg, | ||
}; | ||
} | ||
|
||
function textGradient({ value }: ParserParams<GradientProperty>) { | ||
const baseStyle = gradient({ value } as any); | ||
return { | ||
...baseStyle, | ||
backgroundClip: 'text', | ||
textFillColor: 'transparent', | ||
'-webkit-background-clip': 'text', | ||
'-webkit-text-fill-color': 'transparent', | ||
}; | ||
} | ||
|
||
export const gradientParsers: GradientsParsers = { | ||
gradient: gradient, | ||
bgGradient: gradient, | ||
textGradient: textGradient, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './pseudos'; | ||
export * from './gradients'; |
Oops, something went wrong.