/* eslint-disable max-lines-per-function */
import _ from 'lodash'
import { indent, percents, svgAttrs, validColor } from './utils'
type Angle = number | string
const fromEntries = Object.fromEntries
const join = (...strings: string[]) => strings.filter(String).join('-')
type Gradient = {
colors?: string[]
fallback?: string
angles?: number | number[] | string | string[]
}
type Gradients = { [k: string]: Gradient }
interface GradientPluginOptions {
fallback?: 'current' | 'currentColor' | 'first' | 'last'
svgUrlPrefix?: string
svgIdPrefix?: string
classPrefix?: string
svgClassPrefix?: string
suffix?: string
addSvgInherit?: boolean
onResult?: (svgSource: string, svgDefs: string[]) => void
}
import { TailwindPlugin } from '.'
type GradientPlugin = (options?: GradientPluginOptions) => TailwindPlugin
const linearGradient = (angle: string | number, colors: string[]) =>
`linear-gradient(${angle}deg, ${colors.join(', ')})`
export const tailwindGradients: GradientPlugin = options => {
const {
svgIdPrefix = 'gradient',
svgUrlPrefix = '',
classPrefix = `bg-gradient`,
svgClassPrefix = '',
suffix = ``,
addSvgInherit = true,
onResult = () => null,
fallback: globalFallback = 'false',
} = options || {}
const fallbackTo = (t = globalFallback, colors: string[]) =>
['false', 'none'].includes(t)
? ''
: t === 'first'
? colors[0]
: t === 'last'
? colors[colors.length - 1]
: ['current', 'currentColor', 'true'].includes(t)
? 'currentColor'
: ''
const angleTail = (angle: Angle, isSolo: boolean = false) =>
isSolo ? '' : `${angle}`
const svgUrl = svgUrlPrefix
const prefix = (name: string) => join(suffix, name)
const className = (name: string, angle: Angle, singleAngle: boolean) =>
`.${join(classPrefix, name, angleTail(angle, singleAngle))}`
const svgId = (name: string, angle: Angle, singleAngle: boolean) =>
`${join(svgIdPrefix, name, angleTail(angle, singleAngle))}`
return ({ addBase, addUtilities, e, theme, variants }) => {
const gradients = theme('gradients', {}) as Gradients
const gradientVariants = variants('gradients', ['responsive'])
const svgResult = [] as string[]
const svgInherits = {
'.fill-inherit svg': { fill: 'inherit' },
'.stroke-inherit svg': { stroke: 'inherit' },
}
if (addSvgInherit) {
addBase(svgInherits)
}
const utilities = _.map(
gradients,
(
{ colors = [], angles: anglesInput = ['45'], fallback, ...rest },
nameInput
) => {
if (!colors || colors.length === 0) return {}
const name = prefix(e(nameInput))
const fallBack = fallbackTo(fallback, colors)
const angles: number[] = (typeof anglesInput === 'number' ||
typeof anglesInput === 'string'
? [anglesInput]
: anglesInput
)
.map(a => (typeof a === 'number' ? a : parseFloat(a)))
.filter(n => !isNaN(n))
const isSolo = angles.length === 1
const gradients = angles
.slice(0, 1)
.map(angle => [
className(name, angle, true),
{
...rest,
backgroundImage: linearGradient(angle, colors),
},
])
.concat(
angles.map(angle => [
className(name, angle, false),
{
...rest,
backgroundImage: linearGradient(angle, colors),
},
])
)
const svgUrls = angles.map(angle => [
angle,
`url(${svgUrl}#${svgId(name, angle, isSolo)}) ${fallBack}`.trim(),
])
const svgClassFill = svgUrls.map(([angle, url]) => [
`.${join(svgClassPrefix, 'fill', name, angleTail(angle, isSolo))}`,
{ fill: url },
])
const svgClassStroke = svgUrls.map(([angle, url]) => [
`.${join(svgClassPrefix, 'stroke', name, angleTail(angle, isSolo))}`,
{ stroke: url },
])
const svgGradient = angles.map(a =>
[
``,
indent(
colors
.map(percents)
.filter(([_k, v]) => v && validColor(`${v}`))
.map(([k, v]) => ``)
.join('\n')
),
``,
].join('\n')
)
svgResult.push(...svgGradient)
return fromEntries([...gradients, ...svgClassFill, ...svgClassStroke])
}
)
if (addSvgInherit) {
utilities.push({
'.fill-inherit': { fill: 'inherit' },
'.stroke-inherit': { stroke: 'inherit' },
})
}
addUtilities(utilities, gradientVariants)
const svgDefs = svgResult.join('\n')
onResult(
[
'',
].join('\n'),
svgResult
)
}
}
export default tailwindGradients