Skip to content

Commit 49bf462

Browse files
authored
Support fractional (#5)
1 parent 021a914 commit 49bf462

File tree

11 files changed

+1194
-845
lines changed

11 files changed

+1194
-845
lines changed

.babelrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
# CSS box model 📦
1+
# `css-box-model` 📦
22

3-
> Get detailed [CSS Box Model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model) information about a [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement)
3+
Get accurate and well named [CSS Box Model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model) information about a [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element).
44

5-
This library is useful for when you need to obtain detailed positioning information about an element. Any time you are using [`Element.getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) you might want to consider using `css-box-model` instead to get more detailed information.
5+
[![Build Status](https://travis-ci.org/alexreardon/css-box-model.svg?branch=master)](https://travis-ci.org/alexreardon/css-box-model)
6+
[![npm](https://img.shields.io/npm/v/css-box-model.svg)](https://www.npmjs.com/package/css-box-model)
7+
[![dependencies](https://david-dm.org/alexreardon/css-box-model.svg)](https://david-dm.org/alexreardon/css-box-model)
8+
[![Downloads per month](https://img.shields.io/npm/dm/css-box-model.svg)](https://www.npmjs.com/package/css-box-model)
9+
[![min](https://img.shields.io/bundlephobia/min/css-box-model.svg)](https://www.npmjs.com/package/css-box-model)
10+
[![minzip](https://img.shields.io/bundlephobia/minzip/css-box-model.svg)](https://www.npmjs.com/package/css-box-model)
11+
12+
Any time you are using [`Element.getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) you might want to consider using `css-box-model` instead to get more detailed box model information.
613

714
## Usage
815

@@ -19,39 +26,24 @@ const box: BoxModel = getBox(el);
1926
## Installation
2027

2128
```bash
29+
## yarn
2230
yarn add css-box-model
31+
32+
# npm
33+
npm install css-box-model --save
2334
```
2435

2536
## The [CSS Box Model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model)
2637

38+
![the box model](https://user-images.githubusercontent.com/2182637/46847224-f8a23e80-ce2e-11e8-80d6-0ca62a1871a7.png)
39+
2740
| Box type | Composition |
2841
| ----------- | ----------------------------------- |
2942
| Margin box | margin + border + padding + content |
3043
| Border box | border + padding + content |
3144
| Padding box | padding + content |
3245
| Content box | content |
3346

34-
```
35-
------------------------------------
36-
| MARGIN | (marginBox)
37-
| ------------------------------ |
38-
| | BORDER | | (borderBox)
39-
| | ------------------------ | |
40-
| | | PADDING | | | (paddingBox)
41-
| | | ------------------ | | |
42-
| | | | CONTENT | | | | (contentBox)
43-
| | | | | | | |
44-
| | | | | | | |
45-
| | | | | | | |
46-
| | | ------------------ | | |
47-
| | | | | |
48-
| | ------------------------ | |
49-
| | | |
50-
| ------------------------------ |
51-
| |
52-
| ---------------------------------|
53-
```
54-
5547
This our returned `BoxModel`:
5648

5749
```js
@@ -134,7 +126,7 @@ const getWindowScroll = (): Position => ({
134126

135127
> `(borderBox: AnyRectType, styles: CSSStyleDeclaration) => BoxModel`
136128
137-
This will do the box model calculations without needing to read from the DOM. This is useful if you have already got a `ClientRect` and `CSSStyleDeclaration` as we do not need to recompute it.
129+
This will do the box model calculations without needing to read from the DOM. This is useful if you have already got a `ClientRect` / `DOMRect` and a `CSSStyleDeclaration` as then we can skip computing these values.
138130

139131
```js
140132
const el: HTMLElement = document.getElementById('app');
@@ -154,7 +146,7 @@ type AnyRectType = ClientRect | DOMRect | Rect | Spacing;
154146

155147
> `({ borderBox, margin, border, padding }: CreateBoxArgs) => BoxModel`
156148
157-
Allows you to create a `BoxModel` by passing in a `Rect` like shape and optionally your own `margin`, `border` and or `padding`.
149+
Allows you to create a `BoxModel` by passing in a `Rect` like shape (`AnyRectType`) and optionally your own `margin`, `border` and or `padding`.
158150

159151
```js
160152
type CreateBoxArgs = {|
@@ -182,11 +174,15 @@ const padding: Spacing = {
182174
const box: BoxModel = createBox({ borderBox, padding });
183175
```
184176

177+
## Utility API
178+
179+
> Functions to help you interact with the objects we provide
180+
185181
### `getRect`
186182

187183
> `(spacing: AnyRectType) => Rect`
188184
189-
Given any `Rect` like shape, return a `Rect`
185+
Given any `Rect` like shape, return a `Rect`. Accepts any object that has `top`, `right`, `bottom` and `right` (eg `ClientRect`, `DOMRect`);
190186

191187
```js
192188
const spacing: Spacing = {
@@ -214,3 +210,81 @@ console.log(rect);
214210
}
215211
*/
216212
```
213+
214+
### `expand`
215+
216+
Used to expand a `Spacing`
217+
218+
```js
219+
(target: Spacing, expandBy: Spacing) => Spacing;
220+
```
221+
222+
```js
223+
const original: Spacing = {
224+
top: 10,
225+
left: 11,
226+
right: 21,
227+
bottom: 22,
228+
};
229+
230+
const expandBy: Spacing = {
231+
top: 1,
232+
left: 2,
233+
right: 3,
234+
bottom: 4,
235+
};
236+
237+
const expanded: Spacing = expand(original, expandBy);
238+
239+
console.log(expanded);
240+
241+
/*
242+
{
243+
// pulled back
244+
top: 8,
245+
left: 8
246+
// pushed forward
247+
bottom: 22,
248+
right: 22,
249+
}
250+
*/
251+
```
252+
253+
### `shrink`
254+
255+
Used to shrink a `Spacing`
256+
257+
```js
258+
(target: Spacing, shrinkBy: Spacing) => Spacing;
259+
```
260+
261+
```js
262+
const original: Spacing = {
263+
top: 10,
264+
left: 10,
265+
right: 20,
266+
bottom: 20,
267+
};
268+
269+
const shrinkBy: Spacing = {
270+
top: 2,
271+
left: 2,
272+
right: 2,
273+
bottom: 2,
274+
};
275+
276+
const smaller: Spacing = shrink(original, shrinkBy);
277+
278+
console.log(smaller);
279+
280+
/*
281+
{
282+
// pushed forward
283+
top: 12,
284+
left: 12
285+
// pulled back
286+
bottom: 18,
287+
right: 18,
288+
}
289+
*/
290+
```

babel.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
presets: ['@babel/flow', ['@babel/env', { loose: true }]],
3+
plugins: [
4+
// used for stripping out the `invariant` messages in production builds
5+
'dev-expression',
6+
],
7+
comments: false,
8+
};

package.json

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
{
22
"name": "css-box-model",
33
"version": "1.0.0",
4-
"description": "Returns the css box model for a HTMLElement",
4+
"description": "Get accurate and well named css box model information about an Element 📦",
55
"author": "Alex Reardon <[email protected]>",
66
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/atlassian/react-beautiful-dnd.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/atlassian/react-beautiful-dnd/issues"
13+
},
714
"keywords": [
815
"css",
916
"box model",
17+
"css box model",
18+
"getBoundingClientRect",
19+
"DOMRect",
20+
"ClientRect",
21+
"Rect",
22+
"Spacing",
1023
"DOM"
1124
],
1225
"files": [
@@ -28,20 +41,24 @@
2841
"prepublishOnly": "yarn build"
2942
},
3043
"devDependencies": {
31-
"babel-cli": "^6.26.0",
32-
"babel-core": "^6.26.3",
33-
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
34-
"babel-plugin-transform-object-rest-spread": "^6.26.0",
35-
"babel-preset-env": "^1.7.0",
36-
"babel-preset-flow": "^6.23.0",
37-
"flow-bin": "^0.75.0",
38-
"jest": "^23.2.0",
39-
"prettier": "1.13.7",
44+
"@babel/core": "^7.1.2",
45+
"@babel/preset-env": "^7.1.0",
46+
"@babel/preset-flow": "^7.0.0",
47+
"babel-core": "7.0.0-bridge.0",
48+
"babel-jest": "^23.6.0",
49+
"babel-plugin-dev-expression": "^0.2.1",
50+
"flow-bin": "^0.83.0",
51+
"jest": "^23.6.0",
52+
"prettier": "1.14.3",
4053
"rimraf": "^2.6.2",
41-
"rollup": "^0.62.0",
42-
"rollup-plugin-babel": "^3.0.5",
43-
"rollup-plugin-commonjs": "^9.1.3",
44-
"rollup-plugin-replace": "^2.0.0",
45-
"rollup-plugin-uglify": "^4.0.0"
54+
"rollup": "^0.66.6",
55+
"rollup-plugin-babel": "^4.0.3",
56+
"rollup-plugin-commonjs": "^9.2.0",
57+
"rollup-plugin-node-resolve": "^3.4.0",
58+
"rollup-plugin-replace": "^2.1.0",
59+
"rollup-plugin-uglify": "^6.0.0"
60+
},
61+
"dependencies": {
62+
"tiny-invariant": "^1.0.1"
4663
}
4764
}

rollup.config.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
// @flow
21
import babel from 'rollup-plugin-babel';
32
import commonjs from 'rollup-plugin-commonjs';
43
import replace from 'rollup-plugin-replace';
54
import { uglify } from 'rollup-plugin-uglify';
5+
import resolve from 'rollup-plugin-node-resolve';
66

77
const input = 'src/index.js';
8+
const excludeAllExternals = id => !id.startsWith('.') && !id.startsWith('/');
9+
const extensions = ['.js', '.jsx'];
810

911
export default [
1012
// ESM build
1113
{
1214
input,
1315
output: {
1416
file: 'dist/css-box-model.esm.js',
15-
format: 'es',
17+
format: 'esm',
1618
},
19+
external: excludeAllExternals,
1720
plugins: [babel()],
1821
},
1922
// CommonJS build
2023
{
2124
input,
25+
external: excludeAllExternals,
2226
output: {
2327
file: 'dist/css-box-model.cjs.js',
2428
format: 'cjs',
@@ -34,10 +38,11 @@ export default [
3438
name: 'cssBoxModel',
3539
},
3640
plugins: [
37-
// Setting development env before running babel etc
38-
replace({ 'process.env.NODE_ENV': JSON.stringify('development') }),
3941
babel(),
42+
// used to include tiny-invariant
43+
resolve({ extensions }),
4044
commonjs({ include: 'node_modules/**' }),
45+
replace({ 'process.env.NODE_ENV': JSON.stringify('development') }),
4146
],
4247
},
4348
// Universal module definition (UMD) build (production)
@@ -49,10 +54,11 @@ export default [
4954
name: 'cssBoxModel',
5055
},
5156
plugins: [
52-
// Setting production env before running babel etc
53-
replace({ 'process.env.NODE_ENV': JSON.stringify('production') }),
5457
babel(),
58+
// used to include tiny-invariant
59+
resolve({ extensions }),
5560
commonjs({ include: 'node_modules/**' }),
61+
replace({ 'process.env.NODE_ENV': JSON.stringify('production') }),
5662
uglify(),
5763
],
5864
},

0 commit comments

Comments
 (0)