Skip to content

Commit 640d1b4

Browse files
committed
add getting background position
1 parent 5a63e4e commit 640d1b4

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/css-utils.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function _getAttributeFromString(string, method, ...data) {
1111
}
1212
}
1313

14+
// eslint-disable-next-line no-extend-native
15+
String.prototype.matchesValidCSSLength = () =>
16+
this.match(/(\d+(\.\d+)?(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)|0)/)
17+
1418
function _getColor(b, i) {
1519
const val = b[i]
1620
// color is a hex code
@@ -61,17 +65,50 @@ function _getImageSize(b, i, element) {
6165
return [val, null]
6266
}
6367

68+
return __getTwoNumerics(b, i, element, htmlBackgroundSizeNotSvgError)
69+
}
70+
71+
function _getPosition(b, i, element) {
72+
// "val" is always what is defined in backgrund-size ([i]th argument)
73+
const val = b[i]
74+
const allWords = ['top', 'bottom', 'left', 'right', 'center']
75+
76+
if (b.length === 1 && allWords.includes(val)) {
77+
if (val === 'center')
78+
return ['center', 'center']
79+
else if (['left', 'right'].includes(val))
80+
return [val, 0]
81+
else if (['top', 'bottom'].includes(val))
82+
return [0, val]
83+
}
84+
85+
const a = [0, 0]
86+
87+
if (allWords.includes(val)) {
88+
if (b[i + 1].matchesValidCSSLength()) {
89+
90+
}
91+
}
92+
93+
/*if (['cover', 'contain'].includes(val)) {
94+
return [val, null]
95+
}*/
96+
97+
return __getTwoNumerics(b, i, element, htmlBackgroundPositionNotSvgError)
98+
}
99+
100+
function __getTwoNumerics(b, i, element, err) {
64101
const returnIfCSSNumeric = (val, throwErr) => {
65102
if (val?.endsWith('%'))
66103
return val
67-
else if (val?.match(/(\d+(\.\d+)?(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)|0)/)) {
68-
unitCheck(val, throwErr ? htmlBackgroundSizeNotSvgError : undefined)
104+
else if (val?.matchesValidCSSLength()) {
105+
unitCheck(val, throwErr ? err : undefined)
69106
return toPx(element, val)
70107
} else
71108
return null
72109
}
73110

74-
const convertedVal = returnIfCSSNumeric(val, true) // has null as fallback already
111+
const convertedVal = returnIfCSSNumeric(b[i], true) // has null as fallback already
75112
// "background-size: 50% 50%" is different to "background-size: 50%"
76113
return [convertedVal, returnIfCSSNumeric(b[i + 1])]
77114
}
@@ -108,6 +145,8 @@ const htmlBorderLengthNotSvgError =
108145
new Error(htmlLengthNotSvgErrorTemplate('border lengths', '"thin", "medium", "thick"'))
109146
const htmlBackgroundSizeNotSvgError =
110147
new Error(htmlLengthNotSvgErrorTemplate('background size', '"cover", "contain"'))
148+
const htmlBackgroundPositionNotSvgError =
149+
new Error(htmlLengthNotSvgErrorTemplate('background position', '"top", "bottom", "left", "right", "center"'))
111150

112151
function unitCheck(length, err) {
113152
if (length?.match(/(cap|ic|lh|rlh|vi|vm|vb|Q|mozmm)/g))
@@ -126,7 +165,7 @@ function _getWidth(border, i, element) {
126165
if (val.toLowerCase() === 'thick') return 5
127166
unitCheck(val, htmlBorderLengthNotSvgError)
128167
// width is <length>
129-
if (val.match(/(\d+(\.\d+)?(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)|0)/))
168+
if (val.matchesValidCSSLength())
130169
return toPx(element, val)
131170
return false
132171
}
@@ -137,11 +176,13 @@ const getWidth = (s, el) => _getAttributeFromString(s, _getWidth, el),
137176
getImage = s => _getAttributeFromString(s, _getImage),
138177
/** @returns {Array<string|number>} */
139178
getImageSize = (s, el) => _getAttributeFromString(s, _getImageSize, el),
179+
/** @returns {Array<string|number>} */
180+
getPosition = (s, el) => _getAttributeFromString(s, _getPosition, el),
140181
/** @returns {string} */
141182
getColor = s => _getAttributeFromString(s, _getColor),
142183
/** @returns {Array<string>} */
143184
getRepeat = s => _getAttributeFromString(s, _getRepeat),
144185
/** @returns {number} */
145186
getOpacity = s => _getAttributeFromString(s, _getOpacity)
146187

147-
export {getWidth, getImage, getImageSize, getColor, getRepeat, getOpacity}
188+
export {getWidth, getImage, getImageSize, getPosition, getColor, getRepeat, getOpacity}

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default function RoundDiv({clip, style, children, ...props}) {
1515
const [backgroundImage, setBackgroundImage] = useState('none')
1616
// todo: background size two values (from css)
1717
const [backgroundImageSize, setBackgroundImageSize] = useState([null, null])
18+
const [backgroundPosition, setBackgroundPosition] = useState([0, 0])
1819
const [backgroundOpacity, setBackgroundOpacity] = useState(0)
1920
const [backgroundRepeat, setBackgroundRepeat] = useState(['repeat', 'repeat'])
2021

@@ -39,6 +40,7 @@ export default function RoundDiv({clip, style, children, ...props}) {
3940
setBackground,
4041
setBackgroundImage,
4142
setBackgroundImageSize,
43+
setBackgroundPosition,
4244
setBackgroundOpacity,
4345
setBackgroundRepeat,
4446
setBorderColor,

src/updateStates.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getColor, getImage, getImageSize, getOpacity, getRepeat, getWidth} from "./css-utils";
1+
import {getColor, getImage, getImageSize, getPosition, getOpacity, getRepeat, getWidth} from "./css-utils";
22
import getStyle from "./styles-extractor";
33

44
export default function updateStates(args) {
@@ -55,6 +55,9 @@ export default function updateStates(args) {
5555
states.setBackgroundImageSize(getImageSize(
5656
getNthStyleAttr('background-size', 1)
5757
) || [null, null])
58+
states.setBackgroundImageSize(getPosition(
59+
getNthStyleAttr('background-size', 1)
60+
) || [null, null])
5861
states.setBackgroundOpacity(getOpacity(
5962
getNthStyleAttrOrAlt('background', 'background-color', 1)
6063
) || 1)

0 commit comments

Comments
 (0)