{"id":8532,"date":"2020-08-26T19:49:35","date_gmt":"2020-08-26T19:49:35","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8532"},"modified":"2020-08-27T08:37:52","modified_gmt":"2020-08-27T08:37:52","slug":"resizing-a-component-relative-to-its-parent-position-fixed","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/resizing-a-component-relative-to-its-parent-position-fixed\/","title":{"rendered":"Resizing a component relative to it&#8217;s parent (position: fixed)"},"content":{"rendered":"<p>In the previous post I mentioned how we can fix a footer to the bottom of the viewport, but we have an issue where the width extends outside the width of the parent.<\/p>\n<p>NOTE: This post discusses a <em>partial<\/em> solution to this issue. In that, if the browser window size changes this works, but if your viewport size changes, it doesn&#8217;t. So for example if you have a flyout Drawer component or the likes which takes up space on the page, then no window resize takes place, only a layout change takes place. If I resolve this I&#8217;ll update this post.<\/p>\n<p>To solve this we&#8217;re going to create a <em>ref<\/em> on our parent div and monitor it&#8217;s size changes and then apply them to our footer. <\/p>\n<p>So assuming we&#8217;re using material-ui&#8217;s createStyle, with the following CSS (or use plain CSS or your preferred CSS library)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfooter: {\r\n  position: &quot;fixed&quot;,\r\n  bottom: 0,\r\n  marginBottom: &quot;10px&quot;,\r\n  textAlign: &quot;left&quot;,\r\n},\r\n<\/pre>\n<p>Here&#8217;s an abridged version of my code (using a React functional component)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst targetRef: any = useRef(null);\r\nconst &#x5B;relativeWidth, setRelativeWidth] = useState(0);\r\n\r\nuseLayoutEffect(() =&gt; {\r\n  function updateWidth() {\r\n    if (targetRef != null &amp;&amp; targetRef.current != null) {\r\n      setRelativeWidth(targetRef.current.offsetWidth);\r\n    }\r\n  }\r\n\r\n  window.addEventListener(&quot;resize&quot;, updateWidth);\r\n  updateSize();\r\n  return () =&gt; window.removeEventListener(&quot;resize&quot;, updateWidth);\r\n}, &#x5B;]);\r\n\r\nreturn (\r\n  &lt;div ref={targetRef}&gt;\r\n    {\/* our other elements *\/}\r\n    &lt;div className={classes.footer}\r\n      style={{ width: relativeWidth }}&gt;\r\n      {\/* our footer elements *\/}\r\n    &lt;\/div&gt;\r\n  &lt;\/div&gt;\r\n);\r\n<\/pre>\n<p>So what we&#8217;re doing is getting a <em>ref<\/em> to the parent div and storing the relativeWidth in the component&#8217;s state using useState (which we set up at the start of the function). Using useEffect, we create a listener to the window&#8217;s resize events and then update our state. Finally in the footer style we set the width explicitly using then relativeWidth that we stored.<\/p>\n<p>Not only will this now display correctly relative to the parent div, but also resize relative to it as well.<\/p>\n<p>These are early days for this code, but so far this looks to work a treat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I mentioned how we can fix a footer to the bottom of the viewport, but we have an issue where the width extends outside the width of the parent. NOTE: This post discusses a partial solution to this issue. In that, if the browser window size changes this works, but if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[43,243],"tags":[],"class_list":["post-8532","post","type-post","status-publish","format-standard","hentry","category-css","category-react"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8532"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8532\/revisions"}],"predecessor-version":[{"id":8536,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8532\/revisions\/8536"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}