|
| 1 | +import type { ParseContext, Style, StyleIR } from '../types'; |
| 2 | +import type { TwTheme } from '../tw-config'; |
| 3 | +import { isArbitraryValue, parseNumericValue, parseStyleVal } from '../helpers'; |
| 4 | + |
| 5 | +export function filterBrightness( |
| 6 | + value: string, |
| 7 | + context: ParseContext = {}, |
| 8 | + config?: TwTheme['brightness'], |
| 9 | +): StyleIR | null { |
| 10 | + const styleVal = getBaseFilterStyleValue(value, context, config?.[value]); |
| 11 | + |
| 12 | + return createStyle(`brightness`, styleVal); |
| 13 | +} |
| 14 | + |
| 15 | +export function filterContrast( |
| 16 | + value: string, |
| 17 | + context: ParseContext = {}, |
| 18 | + config?: TwTheme['contrast'], |
| 19 | +): StyleIR | null { |
| 20 | + const styleVal = getBaseFilterStyleValue(value, context, config?.[value]); |
| 21 | + |
| 22 | + return createStyle(`contrast`, styleVal); |
| 23 | +} |
| 24 | + |
| 25 | +export function filterSaturate( |
| 26 | + value: string, |
| 27 | + context: ParseContext = {}, |
| 28 | + config?: TwTheme['saturate'], |
| 29 | +): StyleIR | null { |
| 30 | + const styleVal = getBaseFilterStyleValue(value, context, config?.[value]); |
| 31 | + |
| 32 | + return createStyle(`saturate`, styleVal); |
| 33 | +} |
| 34 | + |
| 35 | +export function filterGrayscale( |
| 36 | + value: string, |
| 37 | + context: ParseContext = {}, |
| 38 | + config?: TwTheme['grayscale'], |
| 39 | +): StyleIR | null { |
| 40 | + const parsed = value.startsWith(`-`) ? value.slice(1) : `100`; |
| 41 | + const styleVal = getPercentageFilterStyleValue(parsed, context, config?.[value]); |
| 42 | + |
| 43 | + return createStyle(`grayscale`, styleVal); |
| 44 | +} |
| 45 | + |
| 46 | +export function filterInvert( |
| 47 | + value: string, |
| 48 | + context: ParseContext = {}, |
| 49 | + config?: TwTheme['invert'], |
| 50 | +): StyleIR | null { |
| 51 | + const parsed = value.startsWith(`-`) ? value.slice(1) : `100`; |
| 52 | + const styleVal = getPercentageFilterStyleValue(parsed, context, config?.[value]); |
| 53 | + |
| 54 | + return createStyle(`invert`, styleVal); |
| 55 | +} |
| 56 | + |
| 57 | +export function filterSepia( |
| 58 | + value: string, |
| 59 | + context: ParseContext = {}, |
| 60 | + config?: TwTheme['sepia'], |
| 61 | +): StyleIR | null { |
| 62 | + const parsed = value.startsWith(`-`) ? value.slice(1) : `100`; |
| 63 | + const styleVal = getPercentageFilterStyleValue(parsed, context, config?.[value]); |
| 64 | + |
| 65 | + return createStyle(`sepia`, styleVal); |
| 66 | +} |
| 67 | + |
| 68 | +export function filterHueRotate( |
| 69 | + value: string, |
| 70 | + context: ParseContext = {}, |
| 71 | + config?: TwTheme['hueRotate'], |
| 72 | +): StyleIR | null { |
| 73 | + const configValue = config?.[value]; |
| 74 | + let styleVal: string | number | null; |
| 75 | + if (configValue) { |
| 76 | + styleVal = parseStyleVal(configValue, context); |
| 77 | + } else if (isArbitraryValue(value)) { |
| 78 | + const parsed = parseNumericValue(value.slice(1, -1)); |
| 79 | + styleVal = parsed ? `${parsed[0]}${parsed[1]}` : null; |
| 80 | + } else { |
| 81 | + const parsed = parseNumericValue(value); |
| 82 | + styleVal = parsed ? `${parsed[0]}${parsed[1]}` : null; |
| 83 | + } |
| 84 | + |
| 85 | + return createStyle(`hueRotate`, styleVal); |
| 86 | +} |
| 87 | + |
| 88 | +function getBaseFilterStyleValue( |
| 89 | + value: string, |
| 90 | + context: ParseContext = {}, |
| 91 | + configValue?: string, |
| 92 | +): string | number | null { |
| 93 | + if (configValue) { |
| 94 | + return parseStyleVal(configValue, context); |
| 95 | + } else if (isArbitraryValue(value)) { |
| 96 | + const parsed = parseNumericValue(value.slice(1, -1)); |
| 97 | + return parsed ? parsed[0] : null; |
| 98 | + } else { |
| 99 | + const parsed = parseNumericValue(value); |
| 100 | + return parsed ? parsed[0] / 100 : null; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function getPercentageFilterStyleValue( |
| 105 | + value: string, |
| 106 | + context: ParseContext = {}, |
| 107 | + configValue?: string, |
| 108 | +): string | number | null { |
| 109 | + if (configValue) { |
| 110 | + const parsed = parseStyleVal(configValue, context)?.toString().slice(0, -1); |
| 111 | + return parsed ? parseInt(parsed) / 100 : null; |
| 112 | + } else if (isArbitraryValue(value)) { |
| 113 | + const parsed = parseNumericValue(value.slice(1, -1)); |
| 114 | + if (parsed === null) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + if (Number.isInteger(parsed[0])) { |
| 118 | + return parsed[0] / 100; |
| 119 | + } |
| 120 | + return parsed[0]; |
| 121 | + } else { |
| 122 | + const parsed = parseNumericValue(value); |
| 123 | + if (parsed === null) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + if (Number.isInteger(parsed[0])) { |
| 127 | + return parsed[0] / 100; |
| 128 | + } |
| 129 | + return parsed[0]; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function createStyle( |
| 134 | + filterType: string, |
| 135 | + styleVal: string | number | null, |
| 136 | +): StyleIR | null { |
| 137 | + return { |
| 138 | + kind: `dependent`, |
| 139 | + complete(style) { |
| 140 | + updateFilterStyle(style, filterType, styleVal); |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +function updateFilterStyle( |
| 146 | + style: Style, |
| 147 | + key: string, |
| 148 | + styleVal: string | number | null, |
| 149 | +): void { |
| 150 | + if (styleVal === null) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + const existingFilter = (style.filter || []) as Style[]; |
| 155 | + if (Array.isArray(existingFilter) && existingFilter) { |
| 156 | + style.filter = [...existingFilter, { [key]: styleVal }]; |
| 157 | + } else { |
| 158 | + style.filter = existingFilter; |
| 159 | + } |
| 160 | +} |
0 commit comments