]> cat aescling's git repositories - mastodon.git/commitdiff
[Glitch] Scroll into search bar when focus
authorJeong Arm <kjwonmail@gmail.com>
Tue, 1 Oct 2019 17:19:10 +0000 (02:19 +0900)
committerThibaut Girka <thib@sitedethib.com>
Fri, 4 Oct 2019 17:22:11 +0000 (19:22 +0200)
Port 66fda37fd04de989d12f3f4c565ba5bfc6ee189d to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
app/javascript/flavours/glitch/features/compose/components/search.js

index 29d195ecd323c8b81edaf2fdc79c797f8d78332c..12d83963788d57c7c425d5a82622d9b03fdae2a2 100644 (file)
@@ -73,12 +73,17 @@ class Search extends React.PureComponent {
     onShow: PropTypes.func.isRequired,
     openInRoute: PropTypes.bool,
     intl: PropTypes.object.isRequired,
+    singleColumn: PropTypes.bool,
   };
 
   state = {
     expanded: false,
   };
 
+  setRef = c => {
+    this.searchForm = c;
+  }
+
   handleChange = (e) => {
     const { onChange } = this.props;
     if (onChange) {
@@ -103,10 +108,14 @@ class Search extends React.PureComponent {
   }
 
   handleFocus = () => {
-    const { onShow } = this.props;
     this.setState({ expanded: true });
-    if (onShow) {
-      onShow();
+    this.props.onShow();
+
+    if (this.searchForm && !this.props.singleColumn) {
+      const { left, right } = this.searchForm.getBoundingClientRect();
+      if (left < 0 || right > (window.innerWidth || document.documentElement.clientWidth)) {
+        this.searchForm.scrollIntoView();
+      }
     }
   }
 
@@ -135,6 +144,7 @@ class Search extends React.PureComponent {
         <label>
           <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
           <input
+            ref={this.setRef}
             className='search__input'
             type='text'
             placeholder={intl.formatMessage(messages.placeholder)}