Skip to content

Commit 0285ffb

Browse files
committed
Documents global stylesheet and adds ScalableRect component
1 parent 0d26207 commit 0285ffb

File tree

8 files changed

+167
-17
lines changed

8 files changed

+167
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Topcoder React Utils Changelog
22

3+
### v0.1.0
4+
The first release of the package. In general, everything is set up, we are going
5+
to move in difference stuff, without changing created package structure.
6+
37
### v0.0.x
48
Pre-release drafts of the initial package version. A big journey starts here.

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ external ReactJS projects developed by the Topcoder community.
1313

1414
### <a name="installation">Installation</a>
1515
Install this package as
16-
```
16+
```bash
1717
$ npm install --save topcoder-react-utils
1818
```
19+
Import the global stylesheet into the root ReactJS component of your app:
20+
```js
21+
import 'topcoder-react-utils/dist/style.css';
22+
```
1923

2024
You are done if you only use components and utilities provided by this
2125
package. If you are to use configurations to build or test your code, you
@@ -39,18 +43,22 @@ configurations for [ESLint](https://eslint.org/);
3943
- [**Webpack Configurations**](docs/webpack-config.md) &mdash; Standard configurations for [Webpack](https://webpack.js.org/).
4044

4145
### <a name="components">Components</a>
42-
- [**Button**](docs/button.md) &mdash; Handles buttons and button-like links
43-
(components that look like regular buttons, but behave as links) in the same
44-
uniform manner;
45-
- [**Link and NavLink**](docs/link-and-navlink.md) &mdash; Auxiliary wrappers
46-
around [React Router](https://github.com/ReactTraining/react-router)'s `<Link>`
47-
and `<NavLink>` components; they help to handle external and internal links in
48-
the same uniform manner.
46+
- [**`Button`**](docs/button.md) &mdash; Handles buttons and button-like links
47+
(components that look like regular buttons, but behave as links) in the same
48+
uniform manner;
49+
- [**`Link` and `NavLink`**](docs/link-and-navlink.md) &mdash; Auxiliary wrappers
50+
around [React Router](https://github.com/ReactTraining/react-router)'s `<Link>`
51+
and `<NavLink>` components; they help to handle external and internal links in
52+
the same uniform manner;
53+
- [**`ScalableRect`**](docs/scalable-rect) &mdash; Container that keeps
54+
the specified aspect ratio regardless its width you set.
55+
4956

5057
### <a name="utilities">Utilities</a>
51-
- [**Global Styles**](docs/global-styles.md) &mdash; TO BE DOCUMENTED;
58+
- [**Global Styles**](docs/global-styles.md) &mdash; Global styles necessary for
59+
a generic application;
5260
- [**SCSS Mixins**](docs/scss-mixins.md) &mdash; Collection of useful style
53-
mixins;
61+
mixins.
5462

5563
### <a name="development">Development</a>
5664
For convenient development you can link this package into your host package:
@@ -80,13 +88,11 @@ organization members and repo admins can commit to `master`) will trigger the
8088
testing, and, if successful, release of the updated package to the NPM registry.
8189

8290
For successful release to NPM you should bump the package version in the
83-
`package.json`. To do it conveniently you can use one of the commands:
84-
- `$ npm version patch`
85-
- `$ npm version minor`
86-
- `$ npm version major`
87-
88-
Say, if the current package version is `v0.1.2` these commands will bump `2`,
89-
`1`, and `0`, correspondingly. Mind that `patch` updates should not introduce
91+
`package.json`. To do it conveniently you can use `$ npm version UPDATE_TYPE`
92+
command, where `UPDATE_TYPE` stays for one of `patch`/`minor`/`major` to bump up
93+
`2`, `1`, or `0` in a sample version number `v0.1.2`. This command will update
94+
`package.json` and `package-lock.json`, and create a new commit and tag in the
95+
checked-out Git branch. Mind that `patch` updates should not introduce
9096
any breaking changes into the codebase! Breaking changes should be done via
9197
`minor` or `major` update, and they should be documented in
9298
the [CHANGELOG](CHANGELOG.md).

docs/global-styles.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Global Styles
2+
Global styles necessary for a generic application.
3+
4+
**Why?** &mdash; There are some standard CSS code we are likely to reuse in any
5+
web app, so here we provide it.
6+
7+
The global stylesheets are include into your app when you add to your root
8+
ReactJS component the import
9+
```js
10+
import 'topcoder-react-utils/dist/style.css';
11+
```
12+
13+
This stylesheet, beside bundled-in styles necessary for components provided by
14+
this package, contains:
15+
16+
- [Eric Meyer's "Reset CSS" 2.0](https://meyerweb.com/eric/tools/css/reset/);
17+
- Enables `geometryPrecision` for the text rendering quality.
18+

docs/scalable-rect.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ScalableRect
2+
Container that keeps the specified aspect ratio regardless its width you set.
3+
4+
[Example](#example)
5+
6+
**Why?** &mdash; From time to time you need to render an HTML element with
7+
a specified aspect ratio of its sizes. HTML/CSS allows to achieve that, but
8+
it demands two nested `<div>` elements, and a non-intuitive CSS code. This
9+
component hides all technical aspects of that solution under the hood.
10+
11+
**Accepted Props:**
12+
- **`children`** &mdash; *Node* &mdash; Optional. Any content you want to render
13+
inside the container. Defaults to *null*;
14+
- **`className`** &mdash; *String* &mdash; Optional. The style to be applied to
15+
the container itself. When provided, the rendered conainer will be wrapped into
16+
an additional `<div>` wrapper, and the style will be applied to that wrapper.
17+
Defaults to *null*;
18+
- **`ratio`** &mdash; *String* &mdash; Optional. Size ratio of the rendered
19+
rectangle, in `W:H` form. Defaults to `1:1`.
20+
- **`styleName`** &mdash; *String* &mdash; Optional. As an alternative to
21+
the `className`, you can pass in `styleName` instead. Babel will convert
22+
it to the correct classname, following our setup for CSS modules.
23+
24+
### <a name="example">Example</a>
25+
```scss
26+
// style.scss
27+
28+
.container {
29+
background: red;
30+
width: 640px;
31+
}
32+
33+
.content {
34+
background: blue;
35+
height: 100%;
36+
margin: 10px;
37+
width: 100;
38+
}
39+
```
40+
41+
```js
42+
// index.scss
43+
44+
import React from 'react';
45+
import { ScalableRect } from 'topcoder-react-utils';
46+
47+
import './style.scss';
48+
49+
export default function Example() {
50+
return (
51+
<ScalableRect styleName="container" ratio="4:3">
52+
<div styleName="content" />
53+
</ScalableRect>
54+
);
55+
}
56+
```

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import Button from 'components/Button';
22
import Link from 'components/Link';
33
import NavLink from 'components/NavLink';
4+
import ScalableRect from 'components/ScalableRect';
45

56
import 'styles/global.scss';
67

78
export {
89
Button,
910
Link,
1011
NavLink,
12+
ScalableRect,
1113
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Creates a dynamically scalable rectangular container with the specified fixed
4+
* ratio of its sides.
5+
*
6+
* Sometimes it is necessary, but not straightforward to implement with HTML and
7+
* CSS, thus this component: juts tell it what sides ration you need (in the
8+
* form W:H, like 4:3 or 16:9 via the "ratio" prop, and pass in the children
9+
* you want to render in this container).
10+
*/
11+
12+
import PT from 'prop-types';
13+
import React from 'react';
14+
15+
import './style.scss';
16+
17+
export default function ScalableRect({ children, className, ratio }) {
18+
const aux = ratio.split(':');
19+
const paddingBottom = `${(100 * aux[1]) / aux[0]}%`;
20+
21+
/* NOTE: In case the following code looks strange to you, mind that we want to
22+
* allow the user to set custom styles on this component. If user passes in a
23+
* "className" prop (possibly "styleName", but that one is converted to
24+
* "className" by Babel just before being passed into this component), it
25+
* should not interfere with the sizing behavior, thus we need an extra <div>
26+
* level in this component; however, if user does not need a custom styling,
27+
* we can save one level of HTML code, so we do it. */
28+
const rect = (
29+
<div
30+
style={{ paddingBottom }}
31+
styleName="container"
32+
><div styleName="wrapper">{children}</div></div>
33+
);
34+
return className ? (
35+
<div className={className}>{rect}</div>
36+
) : rect;
37+
}
38+
39+
ScalableRect.defaultProps = {
40+
children: null,
41+
className: null,
42+
ratio: '1:1',
43+
};
44+
45+
ScalableRect.propTypes = {
46+
children: PT.node,
47+
className: PT.string,
48+
ratio: PT.string,
49+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.container {
2+
position: relative;
3+
width: 100%;
4+
}
5+
6+
.wrapper {
7+
height: 100%;
8+
position: absolute;
9+
width: 100%;
10+
}

src/styles/global.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
/* Global styles. */
22

33
@import "_global/reset";
4+
5+
html,
6+
body {
7+
text-rendering: geometricPrecision;
8+
}

0 commit comments

Comments
 (0)