├── .gitignore ├── .npmignore ├── tsconfig.json ├── index.tsx ├── CHANGELOG.md ├── rollup.config.js ├── LICENSE.md ├── .all-contributorsrc ├── package.json ├── styles.css ├── CODE_OF_CONDUCT.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | 4 | node_modules 5 | dist 6 | .rpt2_cache 7 | 8 | *.orig 9 | *.log 10 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | 4 | node_modules 5 | .rpt2_cache 6 | 7 | *.orig 8 | *.log 9 | 10 | index.tsx 11 | rollup.config.js 12 | tsconfig.json 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "dist/", 4 | "declaration": true, 5 | "declarationDir": "./dist", 6 | "allowSyntheticDefaultImports": true, 7 | "strictNullChecks": true, 8 | "noImplicitAny": true, 9 | "sourceMap": true, 10 | "skipLibCheck": true, 11 | "module": "esnext", 12 | "jsx": "react", 13 | "target": "es5", 14 | "moduleResolution": "node", 15 | "lib": ["es6", "es2016", "dom", "dom.iterable", "scripthost"] 16 | }, 17 | "include": ["*.tsx"], 18 | "exclude": ["node_modules"] 19 | } 20 | -------------------------------------------------------------------------------- /index.tsx: -------------------------------------------------------------------------------- 1 | import React, { HTMLProps } from "react"; 2 | 3 | interface Props extends HTMLProps { 4 | className?: string; 5 | component?: "input" | "textarea"; 6 | } 7 | 8 | const FloatingLabel = ({ component = "input", placeholder, className = "", ...rest }: Props) => ( 9 | 16 | ); 17 | 18 | export default FloatingLabel; 19 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 1.0.0-rc.1 2 | 3 | > 2019-02-04 4 | 5 | #### Breaking changes 6 | 7 | - The component has been rewritten from scratch to be a display component. All internal state is now gone! 8 | - The `element` prop has changed to `component` 9 | - The styling system was stripped out in favor of a simple `className` prop. Much easier and customizeable. 10 | 11 | #### Enhancement 12 | 13 | - This is now written in typescript! Types are now available :) 14 | 15 | #### Bug fixes 16 | 17 | - Any previous styling issues should now be resolved since there is no styling system or default styles. 18 | 19 | # Pre 1.0.0-rc.1 20 | 21 | Lost to history :( 22 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import typescript from "rollup-plugin-typescript2"; 2 | import replace from "rollup-plugin-replace"; 3 | import pkg from "./package.json"; 4 | 5 | export default { 6 | input: "index.tsx", 7 | output: [ 8 | { 9 | file: pkg.main, 10 | format: "cjs", 11 | globals: { react: "React" } 12 | }, 13 | { 14 | file: pkg.module, 15 | format: "es", 16 | globals: { react: "React" } 17 | } 18 | ], 19 | external: ["react"], 20 | plugins: [ 21 | replace({ 22 | // The react sources include a reference to process.env.NODE_ENV so we need to replace it here with the actual value 23 | "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV) 24 | }), 25 | typescript({ 26 | typescript: require("typescript") 27 | }) 28 | ] 29 | }; 30 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 Mike Engel 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "floating-label-react", 3 | "projectOwner": "mike-engel", 4 | "repoType": "github", 5 | "repoHost": "https://github.com", 6 | "files": [ 7 | "README.md" 8 | ], 9 | "imageSize": 100, 10 | "commit": true, 11 | "commitConvention": "angular", 12 | "contributors": [ 13 | { 14 | "login": "mike-engel", 15 | "name": "Mike Engel", 16 | "avatar_url": "https://avatars0.githubusercontent.com/u/464447?v=4", 17 | "profile": "https://www.mike-engel.com", 18 | "contributions": [ 19 | "bug", 20 | "question", 21 | "code", 22 | "doc", 23 | "design", 24 | "ideas", 25 | "infra", 26 | "review", 27 | "test" 28 | ] 29 | }, 30 | { 31 | "login": "bifot", 32 | "name": "Mikhail Semin", 33 | "avatar_url": "https://avatars3.githubusercontent.com/u/5220114?v=4", 34 | "profile": "https://semin.me", 35 | "contributions": [ 36 | "code" 37 | ] 38 | }, 39 | { 40 | "login": "PhallanX14", 41 | "name": "Priyank Trivedi", 42 | "avatar_url": "https://avatars1.githubusercontent.com/u/30751432?v=4", 43 | "profile": "https://github.com/PhallanX14", 44 | "contributions": [ 45 | "code" 46 | ] 47 | } 48 | ], 49 | "contributorsPerLine": 7 50 | } 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "floating-label-react", 3 | "version": "1.0.3", 4 | "description": "A floating-label component using react without any dependencies", 5 | "main": "dist/index.js", 6 | "jsnext:main": "dist/index.es.js", 7 | "module": "dist/index.es.js", 8 | "types": "dist/index.d.ts", 9 | "scripts": { 10 | "build": "rollup -c", 11 | "watch": "rollup -cw", 12 | "prepublishOnly": "npm run build", 13 | "test": "echo \"Error: no test specified\" && exit 1" 14 | }, 15 | "keywords": [ 16 | "floating-label", 17 | "react" 18 | ], 19 | "author": "Mike Engel ", 20 | "license": "MIT", 21 | "devDependencies": { 22 | "@types/react": "^16.9.48", 23 | "react": "^16.13.1", 24 | "rollup": "^3.29.5", 25 | "rollup-plugin-babili": "^3.1.1", 26 | "rollup-plugin-node-resolve": "^5.2.0", 27 | "rollup-plugin-replace": "^2.2.0", 28 | "rollup-plugin-typescript2": "^0.36.0", 29 | "typescript": "^3.9.7" 30 | }, 31 | "peerDependencies": { 32 | "react": ">= 15.x <= 16.x" 33 | }, 34 | "repository": { 35 | "type": "git", 36 | "url": "git+https://github.com/mike-engel/floating-label-react.git" 37 | }, 38 | "bugs": { 39 | "url": "https://github.com/mike-engel/floating-label-react/issues" 40 | }, 41 | "homepage": "https://github.com/mike-engel/floating-label-react#readme" 42 | } 43 | -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | .floating-label { 2 | box-sizing: border-box; 3 | display: block; 4 | width: 100%; 5 | font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, 6 | Fira Sans, Droid Sans, Helvetica Neue, sans-serif; 7 | padding-top: 5px; 8 | position: relative; 9 | } 10 | 11 | .floating-label + .floating-label { 12 | margin-top: 0.5em; 13 | } 14 | 15 | .floating-label input, 16 | .floating-label textarea { 17 | width: 100%; 18 | border: none; 19 | border-bottom: 1px solid black; 20 | box-sizing: border-box; 21 | font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, 22 | Fira Sans, Droid Sans, Helvetica Neue, sans-serif; 23 | font-size: 1rem; 24 | padding: 12px 0 8px 0; 25 | } 26 | 27 | .floating-label input:focus, 28 | .floating-label textarea:focus { 29 | border-color: blue; 30 | } 31 | 32 | .floating-label input:focus + span, 33 | .floating-label textarea:focus + span, 34 | .floating-label.floating span { 35 | font-size: 0.625rem; 36 | padding: 0; 37 | } 38 | 39 | .floating-label input:focus:not(:focus-visible), 40 | .floating-label textarea:focus:not(:focus-visible) { 41 | outline: none; 42 | } 43 | 44 | .floating-label span { 45 | box-sizing: border-box; 46 | font-size: 1rem; 47 | left: 0; 48 | padding: 14px 0 13px 0; 49 | pointer-events: none; 50 | position: absolute; 51 | top: 0; 52 | transition: font-size 200ms, padding 200ms; 53 | z-index: 1; 54 | } 55 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at mike@mike-engel.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # floating-label-react 2 | 3 | > A floating-label component using react without any dependencies 4 | 5 | This react component exists to provide a simple API for floating label inputs in react, while still being fully customizeable. 6 | 7 | # Installation 8 | 9 | Use npm, yarn, or whatever npm package manager variant you use to install `floating-label-react` 10 | 11 | ```sh 12 | # npm 13 | npm install floating-label-react 14 | 15 | # yarn 16 | yarn add floating-label-react 17 | ``` 18 | 19 | # Usage 20 | 21 | `FloatingLabel` takes in most standard [input attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#Attributes) available in HTML5. 22 | 23 | ## Basic usage 24 | 25 | It's important to note that as of 1.0, floating-label-react is now a display component only. This means you need to manage form state via [react-final-form](https://github.com/final-form/react-final-form), [formik](https://jaredpalmer.com/formik/), something similar, your own custom state machine. 26 | 27 | ```js 28 | import FloatingLabel from "floating-label-react"; 29 | 30 | this.setState(() => ({ value: evt.currentTarget.value }))} 37 | />; 38 | ``` 39 | 40 | ## Textarea vs. Input 41 | 42 | If you'd like a textarea instead, use the `component` prop. 43 | 44 | ```js 45 | import FloatingLabel from "floating-label-react"; 46 | 47 | this.setState(() => ({ value: evt.currentTarget.value }))} 55 | />; 56 | ``` 57 | 58 | ## Styling 59 | 60 | `FloatingLabel` is fully stylable through the `className` prop. This works well with vanilla CSS, SASS, Less, styled-components, emotion, etc.! 61 | 62 | ```js 63 | import FloatingLabel from "floating-label-react"; 64 | 65 | this.setState(() => ({ value: evt.currentTarget.value }))} 73 | />; 74 | ``` 75 | 76 | Additionally, a set of default styles are available within the package at `styles.css`. If your bundler support it, you can import them within your application. 77 | 78 | ```js 79 | import FloatingLabel from "floating-label-react"; 80 | import "floating-label-react/styles.css"; 81 | 82 | this.setState(() => ({ value: evt.currentTarget.value }))} 89 | />; 90 | ``` 91 | 92 | # Contributing 93 | 94 | Issues and Pull requests are always welcome. Please keep in mind that there is a code of conduct. 95 | 96 | To get started, install the dev dependencies and build the exports if needed. 97 | 98 | ```sh 99 | # install dependencies 100 | npm install 101 | 102 | # build the project for distribution 103 | npm run build 104 | ``` 105 | 106 | # [Changelog](CHANGELOG.md) 107 | 108 | # [Code of Conduct](CODE_OF_CONDUCT.md) 109 | 110 | # [License](LICENSE.md) 111 | 112 | ## Contributors ✨ 113 | 114 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 |
Mike Engel
Mike Engel

🐛 💬 💻 📖 🎨 🤔 🚇 👀 ⚠️
Mikhail Semin
Mikhail Semin

💻
Priyank Trivedi
Priyank Trivedi

💻
125 | 126 | 127 | 128 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! --------------------------------------------------------------------------------