user9240263
user9240263

Reputation:

How in Reagent with Hiccup make an element with takes all available space

I am trying to find out how in Reagent with Hiccup make an element with takes all available space. So an resize parent I will get :component-did-mount call back.

(defn chart [id col-width row-height]
  (let [dimensions (atom {})]
    (reagent/create-class
      {:component-did-mount
       (fn [e]
         (let [thisComponent (aget (js/document.querySelector ".app") "parentNode")
               width (aget thisComponent "offsetWidth")
               height (aget thisComponent "offsetHeight")]
           (swap! dimensions {:width width :height height})
           (println "----did mountwdth" width "--" height col-width row-height)
           (.log js/console thisComponent)))
       :reagent-render
       (fn [id col-width row-height]
         [:div
          [:div {:style {:background "gray"}} "--drag handle--"]
          [:div.non-dragable
           [simple-bar id]
           [tchart id col-width (int (- row-height controls-height))]]])})))

I want the chart element to take all the space available.

Upvotes: 1

Views: 1079

Answers (2)

Michiel Borkent
Michiel Borkent

Reputation: 34790

I use React Virtualized's AutoSizer for this. Example of integration with Reagent:

(ns example
  (:require
    [cljsjs.react]
    [cljsjs.react-virtualized]
    [goog.object :as gobject]
    [reagent.core :as r]))

(defn autosizer-example
  []
  (r/with-let [width (r/atom 500)
               _ (js/setTimeout #(reset! width 1000)
                                1000)]
    [:div {:style {:width (str @width "px")}}
     [:> js/ReactVirtualized.AutoSizer
      {:disableHeight true
       :disableWidth true}
      (fn [props]
        (let [width (gobject/get props "width")]
          (r/as-element
           [:div
            "Width of parent: " width])))]]))

Docs: https://github.com/bvaughn/react-virtualized/blob/master/docs/AutoSizer.md

Upvotes: 1

Anton Chikin
Anton Chikin

Reputation: 1836

React lifecycle callbacks like ComponentDidMount does not react to component size changes.

If you want to fire a callback whenever the component size changes - you'll need to use some third-party React libraries like react-measure or react-sizeme

The other strategy is to add an event listener on window resize and get your component's parent size from there.

Upvotes: 1

Related Questions