center-center 0.0.37 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.mts +4 -0
  2. package/index.mjs +73 -9
  3. package/package.json +1 -1
package/index.d.mts CHANGED
@@ -4,7 +4,11 @@ export function getLeft (element: Element | SVGElement): number
4
4
  export function getTop (element: Element | SVGElement): number
5
5
  export function getScrollingElement (): ScrollingElement
6
6
  export function getViewportRect (): CenterCenterRect
7
+ export function getDOMElementRect (element: Element): CenterCenterRect
8
+ export function getSVGElementRect (element: SVGElement): CenterCenterRect
7
9
  export function getElementRect (element: Element | SVGElement): CenterCenterRect
10
+ export function getDOMRects (container: Element, target: Element): CenterCenterRects
11
+ export function getSVGRects (container: Element, target: SVGElement): CenterCenterRects
8
12
  export function getRects (container: Element, target: Element | SVGElement): CenterCenterRects
9
13
  export function calculateLeft (rects: CenterCenterRects): number
10
14
  export function calculateTop (rects: CenterCenterRects): number
package/index.mjs CHANGED
@@ -138,16 +138,40 @@ export function getViewportRect () {
138
138
  }
139
139
  }
140
140
 
141
+ /**
142
+ * Gets a `Rect` for an SVG element
143
+ *
144
+ * @param {Element} element
145
+ * @returns {CenterCenterRect}
146
+ */
147
+ export function getSVGElementRect (element) {
148
+ const {
149
+ x: left,
150
+ y: top,
151
+ width,
152
+ height
153
+ } = element.getBBox()
154
+
155
+ return {
156
+ left,
157
+ top,
158
+ width,
159
+ height,
160
+ right: (left + width),
161
+ bottom: (top + height)
162
+ }
163
+ }
164
+
141
165
  /**
142
166
  * Gets a `Rect` for a DOM element
143
167
  *
144
168
  * @param {Element} element
145
169
  * @returns {CenterCenterRect}
146
170
  */
147
- export function getElementRect (element) {
171
+ export function getDOMElementRect (element) {
148
172
  const {
149
- scrollLeft: l,
150
- scrollTop: t
173
+ scrollLeft,
174
+ scrollTop
151
175
  } = getScrollingElement()
152
176
 
153
177
  const {
@@ -157,8 +181,8 @@ export function getElementRect (element) {
157
181
  height
158
182
  } = element.getBoundingClientRect()
159
183
 
160
- const left = l + x
161
- const top = t + y
184
+ const left = (scrollLeft + x)
185
+ const top = (scrollTop + y)
162
186
 
163
187
  return {
164
188
  left,
@@ -171,20 +195,60 @@ export function getElementRect (element) {
171
195
  }
172
196
 
173
197
  /**
174
- * Gets `Rects` for the viewport as well as the container and target elements
198
+ * Gets a `Rect` for a DOM element
199
+ *
200
+ * @param {Element} element
201
+ * @returns {CenterCenterRect}
202
+ */
203
+ export function getElementRect (element) {
204
+ return (
205
+ getDOMElementRect(element)
206
+ )
207
+ }
208
+
209
+ /**
210
+ * Gets `Rects` for the viewport as well as the DOM container and DOM target elements
175
211
  *
176
212
  * @param {Element} container
177
213
  * @param {Element} target
178
214
  * @returns {CenterCenterRects}
179
215
  */
180
- export function getRects (container, target) {
216
+ export function getSVGRects (container, target) {
217
+ return {
218
+ viewport: getViewportRect(),
219
+ container: getDOMElementRect(container),
220
+ target: getSVGElementRect(target)
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Gets `Rects` for the viewport as well as the DOM container and DOM target elements
226
+ *
227
+ * @param {Element} container
228
+ * @param {Element} target
229
+ * @returns {CenterCenterRects}
230
+ */
231
+ export function getDOMRects (container, target) {
181
232
  return {
182
233
  viewport: getViewportRect(),
183
- container: getElementRect(container),
184
- target: getElementRect(target)
234
+ container: getDOMElementRect(container),
235
+ target: getDOMElementRect(target)
185
236
  }
186
237
  }
187
238
 
239
+ /**
240
+ * Gets `Rects` for the viewport as well as the container and target elements
241
+ *
242
+ * @param {Element} container
243
+ * @param {Element} target
244
+ * @returns {CenterCenterRects}
245
+ */
246
+ export function getRects (container, target) {
247
+ return (
248
+ getDOMRects(container, target)
249
+ )
250
+ }
251
+
188
252
  /**
189
253
  * Calculates the target `left` position
190
254
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "center-center",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "Position an element at the center center of a container",
5
5
  "main": "./index.mjs",
6
6
  "type": "module",