screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const MaxInputLength = 100; const { useState, useEffect } = React; function App() { const [inputValue, setInputValue] = useState(''); const adjustWidth = (event) => { const value = event.target.value; if (value.length === 0) { event.target.style.width = 'initial'; } else if (value.length > 20 && value.length < MaxInputLength) { event.target.style.width = `${value.length * 6 + 30}px`; // 5 * 6 + 30 for extra space } setInputValue(value); }; return ( <div className='container'> <h2 className='header'>React Js Increase input width dynamically:</h2> <input type="text" className='input' placeholder="Type something.." value={inputValue} onChange={adjustWidth} /> </div> ); ; } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; max-width: 600px; margin: 0 auto; } .input { border: 2px solid #ccc; border-radius: 5px; padding: 10px; font-size: 16px; transition: width 0.3s ease; /* Smooth width transition */ width: 200px; max-width: 100%; /* Prevent input from overflowing the container */ } </style> </body> </html>