Skip to content

Commit

Permalink
added ssr support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvasilchuk committed Sep 14, 2019
1 parent b78c3e9 commit 810909b
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 245 deletions.
20 changes: 10 additions & 10 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": false
}
]
]
}
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": false
}
]
]
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
node_modules
dist
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
node_modules
assets
40 changes: 20 additions & 20 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2019 Andrew Vasilchuk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
MIT License
Copyright (c) 2019 Andrew Vasilchuk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
170 changes: 85 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
# Vue.js aspect ratio

![Computer screens with different aspect ratios](./assets/img.jpg)

## 💿 Installation

### Via NPM

```bash
$ npm install v-aspect-ratio --save
```
### Via Yarn

```bash
$ yarn add v-aspect-ratio
```

## Initialization

### As a global plugin

It must be called before new Vue ()

```javascript
import Vue from "vue";
import AspectRatio from "v-aspect-ratio";

Vue.use(AspectRatio);
```

### As a global directive

```javascript
import Vue from "vue";
import AspectRatio from "v-aspect-ratio";

Vue.directive("aspect-ratio", AspectRatio.directive)
```

### As a local directive

```javascript
import AspectRatio from "v-aspect-ratio";

export default {
name: "YourAwesomeComponent",
directives: {
"aspect-ratio": AspectRatio.directive
}
}
```

## 🚀 Usage

There's nothing you need to do in JavaScript except for installation. To use the plugin, simply use the v-aspect-ratio directive with the value you need.

```vue
<template>
<div>
<div v-aspect-ratio="'16:9'"></div>
<div v-aspect-ratio="'4:3'"></div>
</div>
</template>
```

## Demo

[![Edit Vue Template](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/ko425ro4k7)

## 💉 Tests
Jest is used for unit-tests.

You can run tests by typing this command in your console:

```bash
npm run test
```

## Powered by

* Babel
* Webpack 4

## 🔒 License

# Vue.js aspect ratio

![Computer screens with different aspect ratios](./assets/img.jpg)

## 💿 Installation

### Via NPM

```bash
$ npm install v-aspect-ratio --save
```
### Via Yarn

```bash
$ yarn add v-aspect-ratio
```

## Initialization

### As a global plugin

It must be called before new Vue ()

```javascript
import Vue from "vue";
import AspectRatio from "v-aspect-ratio";

Vue.use(AspectRatio);
```

### As a global directive

```javascript
import Vue from "vue";
import AspectRatio from "v-aspect-ratio";

Vue.directive("aspect-ratio", AspectRatio.directive)
```

### As a local directive

```javascript
import AspectRatio from "v-aspect-ratio";

export default {
name: "YourAwesomeComponent",
directives: {
"aspect-ratio": AspectRatio.directive
}
}
```

## 🚀 Usage

There's nothing you need to do in JavaScript except for installation. To use the plugin, simply use the v-aspect-ratio directive with the value you need.

```vue
<template>
<div>
<div v-aspect-ratio="'16:9'"></div>
<div v-aspect-ratio="'4:3'"></div>
</div>
</template>
```

## Demo

[![Edit Vue Template](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/ko425ro4k7)

## 💉 Tests
Jest is used for unit-tests.

You can run tests by typing this command in your console:

```bash
npm run test
```

## Powered by

* Babel
* Webpack 4

## 🔒 License

[MIT](http://opensource.org/licenses/MIT)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "v-aspect-ratio",
"version": "1.0.4",
"version": "1.1.0",
"description": "Aspect ratio directive for Vue.js",
"main": "dist/v-aspect-ratio.js",
"scripts": {
"build": "webpack",
"build:ssr": "webpack --config webpack.config.ssr.js",
"test": "jest"
},
"repository": {
Expand All @@ -16,6 +17,10 @@
"aspect-ratio"
],
"author": "Andrew Vasilchuk <andrew.d.vasilchuk@gmail.com>",
"files": [
"src",
"dist"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/3vilArthas/v-aspect-ratio/issues"
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
pattern: /^\d+:\d+$/
}
27 changes: 27 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import config from './config'

export function applyDirective(value, style) {
if (!value) {
throw new Error("No value provided");
}

if (typeof value !== "string") {
throw new Error(
`Expected directive binding value type to be a string but found a ${typeof value} instead`
);
}

if (!config.pattern.test(value)) {
throw new Error(
`Expected directive value to match pattern "[Integer]:[Integer]" gut got a ${value}`
);
}

const values = value.split(":").map(Number);

if (values[0] === 0) {
throw new Error("Division by 0");
}

style.paddingBottom = `${(values[1] / values[0]) * 100}%`;
}
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import directive from "./v-aspect-ratio";

const plugin = {
install(Vue) {
Vue.directive("aspect-ratio", directive);
},
directive
};

export default plugin;
import directive from "./v-aspect-ratio";

const plugin = {
install(Vue) {
Vue.directive("aspect-ratio", directive);
},
directive
};

export default plugin;
3 changes: 3 additions & 0 deletions src/index.ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import directive from "./v-aspect-ratio.ssr";

export default directive;
34 changes: 5 additions & 29 deletions src/v-aspect-ratio.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
const pattern = /^\d+:\d+$/;

export default function(el, binding) {
const { value } = binding;

if (!value) {
throw new Error("No value provided");
}

if (typeof value !== "string") {
throw new Error(
`Expected directive binding value type to be a string but found a ${typeof value} instead`
);
}

if (!pattern.test(value)) {
throw new Error(
`Expected directive value to match pattern "[Integer]:[Integer]" gut got a ${value}`
);
}

const values = value.split(":").map(Number);

if (values[0] === 0) {
throw new Error("Division by 0");
}

el.style.paddingBottom = `${(values[1] / values[0]) * 100}%`;
}
import { applyDirective } from "./helpers";

export default function(el, binding) {
applyDirective(binding.value, el.style);
}
7 changes: 7 additions & 0 deletions src/v-aspect-ratio.ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyDirective } from "./helpers";

export default function aspectRatio(vnode, directiveMeta) {
const style = vnode.data.style || (vnode.data.style = {});

applyDirective(directiveMeta.value, style);
}
Loading

0 comments on commit 810909b

Please sign in to comment.