Skip to content

Commit 31ee8bc

Browse files
committed
Initial Commit
1 parent 3352d0b commit 31ee8bc

11 files changed

+222
-47
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist/
2+
/*.js

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
node_modules
3-
/dist
43

54
# local env files
65
.env.local
@@ -19,3 +18,5 @@ yarn-error.log*
1918
*.njsproj
2019
*.sln
2120
*.sw*
21+
.idea/
22+
*.iml

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright 2018-present Jayesh Vachhani
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

README.md

+81-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,91 @@
1-
# vue-resize-text
1+
# Vue Resize Text
22

3-
## Project setup
4-
```
5-
npm install
6-
```
3+
A vue directive which automatically resize font size based on element width.
74

8-
### Compiles and hot-reloads for development
9-
```
10-
npm run serve
11-
```
5+
It makes the font-size flexible on fluid or responsive layout.
126

13-
### Compiles and minifies for production
14-
```
15-
npm run build
7+
[Live Demo](https://jayeshlab.github.io/vue-resize-text/): Resize the browser viewport to see the effect in action
8+
9+
## Installation
10+
11+
Install via NPM
12+
13+
`$ npm install vue-resize-text --save`
14+
15+
Install via CDN
16+
17+
```html
18+
<script src="https://unpkg.com/vue"></script>
19+
<script src="https://unpkg.com/vue-resize-text"></script>
1620
```
1721

18-
### Lints and fixes files
22+
#### Global
23+
24+
Register VueResizeText globally:
25+
26+
```javascript
27+
import Vue from 'Vue';
28+
import VueResizeText from 'vue-resize-text';
29+
30+
Vue.use(VueResizeText)
1931
```
20-
npm run lint
32+
Directive ```v-resize-text``` then can be used in any of Component
33+
34+
```html
35+
<template>
36+
<div v-resize-text>Hello Vue</div>
37+
</template>
2138
```
2239

23-
### Run your unit tests
40+
#### Local
41+
42+
Include the VueResizeText directive directly into your component using import:
43+
44+
```html
45+
<template>
46+
<div v-resize-text>Hello Vue</div>
47+
</template>
48+
<script>
49+
import ResizeText from 'vue-resize-text'
50+
export default {
51+
directives: {
52+
ResizeText
53+
}
54+
}
55+
</script>
2456
```
25-
npm run test:unit
57+
58+
### Usage
59+
60+
#### Basic usage
61+
62+
```html
63+
<template>
64+
<div>
65+
<div v-resize-text="{ratio:1.3, minFontSize: '30px', maxFontSize: '100px', delay: 200}">Hello Vue</div>
66+
</div>
67+
</template>
68+
69+
<script>
70+
import ResizeText from 'vue-resize-text'
71+
export default {
72+
directives: {
73+
ResizeText
74+
}
75+
};
76+
</script>
2677
```
78+
79+
### Directive Arguments
80+
`v-resize-text="{ratio:1.5, minFontSize: '30px', maxFontSize: '100px', delay: 200}"`
81+
82+
| Argument | Description | Type | Default |
83+
| ----------- | --------------- | ------------ | ------------ |
84+
| ratio | Font Ratio is the tweek to make the text resize properly, greater then `1` makes the font smaller and less then `1` make the font bigger | Number | 1 |
85+
| minFontSize | Minimum font-size threshold in px| Number/String | 16px or 16 | - |
86+
| maxFontSize | Maximum font-size threshold in px| Number/String | 500px or 500 | - |
87+
| delay | Debound time delay on window resize | Number | 200 | - |
88+
89+
## License
90+
91+
MIT

babel.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
presets: [
3-
'@vue/app'
3+
['@vue/app', {
4+
modules: "commonjs"
5+
}]
46
]
57
}

package.json

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
{
22
"name": "vue-resize-text",
33
"version": "0.1.0",
4-
"private": true,
4+
"description": "Vue Resize Text makes font auto resizing on responsive layout",
5+
"author": "JayeshLab <[email protected]>",
6+
"license": "MIT",
7+
"keywords": [
8+
"text",
9+
"resize",
10+
"font",
11+
"responsive",
12+
"fit",
13+
"rescale",
14+
"vue",
15+
"vue.js"
16+
],
17+
"main": "dist/vue-resize-text.common.js",
18+
"unpkg": "dist/vue-resize-text.umd.min.js",
519
"scripts": {
620
"serve": "vue-cli-service serve",
7-
"build": "vue-cli-service build",
21+
"build:docs": "vue-cli-service build --dest docs --name vue-resize-text src/main.js",
822
"lint": "vue-cli-service lint",
23+
"lib": "vue-cli-service build --target lib --name vue-resize-text src/index.js",
924
"test:unit": "vue-cli-service test:unit"
1025
},
11-
"dependencies": {
12-
"vue": "^2.5.17"
26+
"homepage": "https://github.com/JayeshLab/vue-resize-text/#readme",
27+
"repository": {
28+
"type": "git",
29+
"url": "git+https://github.com/JayeshLab/vue-resize-text.git"
30+
},
31+
"bugs": {
32+
"url": "https://github.com/JayeshLab/vue-resize-text/issues"
1333
},
1434
"devDependencies": {
1535
"@vue/cli-plugin-babel": "^3.0.2",
@@ -22,6 +42,13 @@
2242
"babel-jest": "^23.6.0",
2343
"eslint": "^5.8.0",
2444
"eslint-plugin-vue": "^5.0.0-0",
25-
"vue-template-compiler": "^2.5.17"
26-
}
45+
"vue-template-compiler": "^2.5.17",
46+
"vue": "^2.5.17"
47+
},
48+
"files": [
49+
"dist/**",
50+
"src/*",
51+
"*.json",
52+
"*.js"
53+
]
2754
}

src/Demo.vue

+25-12
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,53 @@
55
<h6> A vue directive which automatically resize font size based on element width.</h6>
66
<h6><u>Resize the browser viewport to see the effect in action.</u></h6>
77
</div>
8-
<div v-resize-text="{ratio: 1}">Hello World</div>
9-
<pre>&lt;div v-resize-text="{ratio: 1}"&gt;Hello World&lt;/div&gt;</pre>
10-
<div v-resize-text="{ratio: 1.2}">Hello World</div>
11-
<div v-resize-text="{ratio: 1.5}">Hello World</div>
8+
<hr>
9+
<div v-resize-text="{ratio: 0.6}">Hello Vue</div>
10+
<pre><code>&lt;div v-resize-text="{ratio: 0.6}"&gt;Hello Vue&lt;/div&gt;</code></pre>
11+
<br>
12+
<hr>
13+
<div v-resize-text="{minFontSize: '50px', maxFontSize: '100px'}">Hello Vue</div>
14+
<pre><code>
15+
&lt;div v-resize-text="{minFontSize: '50px', maxFontSize: '100px'}"&gt;
16+
Hello Vue
17+
&lt;/div&gt;
18+
</code></pre>
19+
<br>
20+
<hr>
21+
<div v-resize-text="{ratio: 1.5, delay: 500}">Hello Vue</div>
22+
<pre><code>&lt;div v-resize-text="{ratio: 1.5, delay: 500}"&gt;Hello Vue&lt;/div&gt;</code></pre>
1223
</div>
1324
</template>
1425

1526
<script>
16-
import ResizeText from '../dist/js/app.js'
1727
export default {
18-
name: 'app',
19-
directives: {
20-
ResizeText
21-
}
28+
name: 'app'
2229
}
2330
</script>
2431
<style>
32+
body {
33+
font-size: 16px;
34+
}
2535
#app {
2636
font-family: 'Avenir', Helvetica, Arial, sans-serif;
2737
-webkit-font-smoothing: antialiased;
2838
-moz-osx-font-smoothing: grayscale;
2939
text-align: center;
3040
color: #2c3e50;
31-
margin-top: 60px;
41+
margin-top: 30px;
3242
}
3343
h1 {
3444
font-size: 1em;
3545
margin: 20px 0;
3646
}
3747
h6 {
38-
font-size: .25em;
48+
font-size: .26em;
3949
margin: 10px;
4050
}
51+
p {
52+
font-size: .5em;
53+
}
4154
pre {
42-
font-size: 10px;
55+
font-size: 12px;
4356
}
4457
</style>

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import VueResizeText from './VueResizeText.js'
2+
3+
const install = function(Vue) {
4+
Vue.directive('ResizeText', VueResizeText);
5+
};
6+
7+
if (window.Vue) {
8+
window.VueResizeText = VueResizeText;
9+
Vue.use(install); // eslint-disable-line
10+
}
11+
12+
VueResizeText.install = install;
13+
14+
export default VueResizeText;

src/main.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Vue from 'vue'
2-
import App from './App.vue'
2+
import Demo from './Demo.vue'
3+
import VueResizeText from './index.js'
34

45
Vue.config.productionTip = false
6+
Vue.use(VueResizeText)
57

68
new Vue({
7-
render: h => h(App),
9+
render: h => h(Demo),
810
}).$mount('#app')

tests/unit/vue-resize-text.spec.js

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
1-
import { shallowMount } from '@vue/test-utils'
2-
import HelloWorld from '@/components/HelloWorld.vue'
1+
import Vue from 'vue'
2+
import { mount } from '@vue/test-utils'
3+
import VueResizeText from '@/index.js'
34

4-
describe('HelloWorld.vue', () => {
5-
it('renders props.msg when passed', () => {
6-
const msg = 'new message'
7-
const wrapper = shallowMount(HelloWorld, {
8-
propsData: { msg }
5+
Vue.use(VueResizeText)
6+
7+
8+
describe('v-resize-text', () => {
9+
it('it has inserted, unbind methods available', () => {
10+
expect(typeof VueResizeText.inserted).toBe('function')
11+
expect(typeof VueResizeText.unbind).toBe('function')
12+
})
13+
it('should set default min font size to the element', () => {
14+
const inserted = VueResizeText.inserted
15+
const div = document.createElement('div')
16+
inserted(div, {})
17+
expect(div.style.fontSize).toBe('16px')
18+
})
19+
it('should set to minFontSize for element if specified', (done) => {
20+
const component = {
21+
template: '<div v-resize-text="{ ratio: 2, minFontSize: \'30px\', maxFontSize: \'100px\'}"></div>',
22+
directives: {
23+
ResizeText: VueResizeText
24+
}
25+
}
26+
const wrapper = mount(component)
27+
wrapper.vm.$nextTick(() => {
28+
expect(wrapper.find('div').element.style.fontSize).toBe('30px')
29+
done()
930
})
10-
expect(wrapper.text()).toMatch(msg)
1131
})
12-
})
32+
it('should add event listener on inserted and remove event listener on unbind', ()=> {
33+
const callback = jest.fn();
34+
window.addEventListener = jest.fn();
35+
window.removeEventListener = jest.fn();
36+
const div = document.createElement('div');
37+
VueResizeText.inserted(div, {value: callback});
38+
expect(window.addEventListener).toHaveBeenCalledTimes(1);
39+
VueResizeText.unbind(div);
40+
expect(window.removeEventListener).toHaveBeenCalledTimes(1)
41+
})
42+
})

vue.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
baseUrl: '',
3+
productionSourceMap: false,
4+
filenameHashing: false,
5+
configureWebpack: {
6+
output: {
7+
libraryExport: 'default'
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)