├── .gitignore ├── LICENSE ├── README.md ├── index.ts ├── package.json ├── stretchy.directive.ts └── stretchy.module.ts /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/node 3 | 4 | ### Node ### 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | 24 | # nyc test coverage 25 | .nyc_output 26 | 27 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 28 | .grunt 29 | 30 | # Bower dependency directory (https://bower.io/) 31 | bower_components 32 | 33 | # node-waf configuration 34 | .lock-wscript 35 | 36 | # Compiled binary addons (http://nodejs.org/api/addons.html) 37 | build/Release 38 | 39 | # Dependency directories 40 | node_modules/ 41 | jspm_packages/ 42 | 43 | # Typescript v1 declaration files 44 | typings/ 45 | 46 | # Optional npm cache directory 47 | .npm 48 | 49 | # Optional eslint cache 50 | .eslintcache 51 | 52 | # Optional REPL history 53 | .node_repl_history 54 | 55 | # Output of 'npm pack' 56 | *.tgz 57 | 58 | # Yarn Integrity file 59 | .yarn-integrity 60 | 61 | # dotenv environment variables file 62 | .env 63 | 64 | # idea 65 | .idea 66 | 67 | # End of https://www.gitignore.io/api/node -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Tudor Gergely 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # angular2-stretchy 2 | 3 | ***angular2-stretchy*** is an Angular2 directive that automatically adjusts input width to fit content. 4 | 5 | ## Demo: 6 | 7 | https://tudorgergely.github.io/angular2-stretchy/ 8 | 9 | ## Installation: 10 | 11 | ```bash 12 | npm install angular2-stretchy 13 | ``` 14 | 15 | ## Use Example: 16 | 17 | Add the declaration to your @NgModule: 18 | 19 | ```typescript 20 | import {StretchyModule} from 'angular2-stretchy'; 21 | 22 | ... 23 | 24 | @NgModule({ 25 | imports: [ 26 | StretchyModule 27 | ] 28 | }) 29 | ``` 30 | 31 | Use directly inside your HTML templates 32 | 33 | ``` 34 | 35 | ``` 36 | 37 | Set normal styling on input such as min-width 38 | 39 | ``` 40 | 41 | ``` 42 | 43 | ## Author 44 | 45 | [Tudor Gergely](https://github.com/tudorgergely) 46 | 47 | ## Licence 48 | 49 | This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info. 50 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | export {StretchyDirective} from './stretchy.directive'; 2 | export {StretchyModule} from './stretchy.module'; 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular2-stretchy", 3 | "version": "1.0.1", 4 | "description": "Angular 2 directive to make input have a dynamic width", 5 | "main": "index.ts", 6 | "author": "Tudor Gergely ", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/tudorgergely/angular2-stretchy.git" 11 | }, 12 | "keywords": [ 13 | "ng2", 14 | "angular", 15 | "angular2", 16 | "autosize", 17 | "input", 18 | "stretchy", 19 | "width", 20 | "@angular" 21 | ], 22 | "devDependencies": { 23 | "@angular/core": "^2.4.9", 24 | "typescript": "^2.2.1" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stretchy.directive.ts: -------------------------------------------------------------------------------- 1 | import {Directive, HostBinding, OnInit, Renderer, ElementRef, HostListener} from "@angular/core"; 2 | 3 | @Directive({ 4 | selector: '[stretchy]' 5 | }) 6 | export class StretchyDirective implements OnInit { 7 | @HostBinding('style.width.px') 8 | private inputWidth: number = 0; 9 | 10 | @HostBinding('style.box-sizing') 11 | private get boxSizing() { 12 | return 'content-box'; 13 | } 14 | 15 | private sizeDiv; 16 | 17 | constructor(private renderer: Renderer, 18 | private elementRef: ElementRef) { 19 | } 20 | 21 | ngOnInit() { 22 | this.sizeDiv = this.renderer.createElement(this.elementRef.nativeElement.parentNode, 'div'); 23 | 24 | const inputStyle = window.getComputedStyle(this.elementRef.nativeElement); 25 | Object.assign(this.sizeDiv.style, { 26 | visibility: 'hidden', 27 | position: 'absolute', 28 | top: '0', 29 | left: '0', 30 | whiteSpace: 'pre', 31 | fontSize: inputStyle.fontSize, 32 | fontFamily: inputStyle.fontFamily, 33 | fontWeight: inputStyle.fontWeight, 34 | fontStyle: inputStyle.fontStyle, 35 | letterSpacing: inputStyle.letterSpacing 36 | }); 37 | } 38 | 39 | @HostListener('input', ['$event']) 40 | onKeyDown(e: KeyboardEvent) { 41 | this.sizeDiv.innerText = (e.target).value; 42 | this.inputWidth = this.sizeDiv.getBoundingClientRect().width; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /stretchy.module.ts: -------------------------------------------------------------------------------- 1 | import {StretchyDirective} from "./stretchy.directive"; 2 | import {NgModule} from "@angular/core"; 3 | 4 | @NgModule({ 5 | declarations: [ 6 | StretchyDirective 7 | ], 8 | exports: [ 9 | StretchyDirective 10 | ] 11 | }) 12 | export class StretchyModule { } 13 | --------------------------------------------------------------------------------