@wix/design-system
Version:
@wix/design-system
57 lines • 1.42 kB
JavaScript
import gradient from 'gradient-parser';
import resolveColor from 'color';
import { isUrl } from '../utils/UrlUtils';
const parseVar = (fill) => {
if (fill?.trim().startsWith('var(')) {
return fill;
}
return;
};
export const parseColor = (fill) => {
try {
return resolveColor(fill);
}
catch {
return;
}
};
export const parseGradient = (fill) => {
try {
return gradient.parse(fill);
}
catch {
return;
}
};
export const parseUrl = (fill) => {
if (isUrl(fill)) {
return fill;
}
if (/.jpg|.png|.svg/.test(fill)) {
return `${location.hostname}/${fill}`;
}
return;
};
export const getBackgroundStyles = (fill) => {
if (Array.isArray(fill)) {
return fill.slice(0, 4).map((color) => ({ backgroundColor: color }));
}
if (typeof fill === 'string' && parseUrl(fill)) {
return {
backgroundImage: `url('${fill}')`,
backgroundSize: 'cover',
backgroundPosition: 'center center',
backgroundRepeat: 'no-repeat',
};
}
if (typeof fill === 'string' && (parseVar(fill) || parseColor(fill))) {
return [{ backgroundColor: fill }];
}
if (typeof fill === 'string' && parseGradient(fill)) {
return {
backgroundImage: fill,
};
}
return {};
};
//# sourceMappingURL=FillPreview.utils.js.map