Fork me on GitHub
#reagent
<
2021-05-24
>
zackteo12:05:51

Does anyone have an example of using leaflet from clojurescript? Am using `reagent` I can't quite understand how to get a minimal example working via shadow-cljs

p-himik12:05:56

Is there something that you have tried but doesn't work?

tatut12:05:29

https://github.com/tatut/reagent-leaflet <- very old, don’t know how well that works nowadays

zackteo12:05:36

@U2FRKM4TW have been looking at cljs repo that have used leaflet, but most examples are quite dated. I'm not too sure how to go about initialising a div for the leaflet map component and then specifying the map to correspond to that div

tatut12:05:42

you either use ready made react components like https://react-leaflet.js.org or you call the leaflet methods from you component init

zackteo12:05:31

was planning on using react-leaflet but is MapContainer something I can import and then call from hiccup? Don't quite understand how to go about doing it

p-himik12:05:06

I think you should go through Reagent examples. I'm not sure whether Leaflet specifically is handled there, but pretty much all of the major cases of interop with the JS world are there.

p-himik13:05:36

I really doubt there's any use in Reagent-specific wrappers around JS libraries. More often than not they don't bring anything special to the table or are straight up abandoned. There's some value in React-specific wrappers, if they are maintained. Because it's trivial to use React components from Reagent and you won't have to deal with the state yourself.

p-himik13:05:17

So to answer your question about MapContainer - yes, it is possible. Importing and using React components in Reagent is well polished, well documented, and there's plethora of examples.

zackteo13:05:41

I was trying to do

(ns ... (:require [reagent.core :as reagent]
            [reagent.dom :as rdom]
            ["leaflet-react" :refer MapContainer]))
and
(rdom/render [:div
                [:h3 "Leaflet map"]
                [MapContainer]]
               (.getElementById js/document "app"))
but I don't think it works that way

zackteo13:05:15

Is there a specific place for where the major cases of JS interop with Reagent are documented ?

zackteo13:05:25

Thanks! I seem to have missed that

juhoteperi13:05:51

One example with React-leaflet, but not sure if I'd use that now. It might have had some problems as I've recently just used it directly: https://github.com/metosin/komponentit/blob/master/example-src/cljs/example/map.cljs

zackteo13:05:37

ohhh! I think reagent/adapt-react-class was what I needed ooo

emccue13:05:56

(rdom/render [:div
                [:h3 "Leaflet map"]
                [:> MapContainer]]
               (.getElementById js/document "app"))

emccue13:05:14

that :> works as a shortcut

zackteo13:05:01

let me try .... 🙂

p-himik13:05:07

And it is all in the documentation that I linked. :)

zackteo14:05:16

Is there a way to resize an element in hiccup?

zackteo14:05:10

I think i got leaflet working - but the tilelayer I added is behaving very very strangely. Like it is all jumbled up - not too sure what might be causing it to behave that way

p-himik14:05:00

Yes, depending on what kind of element it is. Same as with the initial problem. Figure out what's wrong using the DevTools inspector, then see how it should be fixed via the component API or the surrounding markup.

zackteo14:05:39

I did

(rdom/render [:div
                [:h1 "Leaflet map"]
                ;; [:> Map
                ;;  {:center [65.6583 26.1475]}]
                [:> MapContainer {:center [1.352, 103.851] :zoom 12 :scrollWheelZoom false}
                 [:> TileLayer {:attribution "&copy; <a href=\"\">OpenStreetMap</a> contributors"
                                :url "https://{s}.}]]]
               (.getElementById js/document "app"))

zackteo14:05:35

but not really sure how to debug something like this

p-himik14:05:38

Doesn't look like a sizing problem, unless each tile is a separate HTML block element. Looks more like an X/Y/Z coordinate issue problem, where some of the coordinates are misplaced.

p-himik14:05:46

Can you provide a bigger picture that includes full tiles?

zackteo14:05:33

I checked the tile-layer url "https://{s}." and it seems to be working fine in other places

zackteo14:05:08

Unless I'm somehow having issues with react-leaflet :thinking_face:

p-himik14:05:22

What do you mean by "other places" exactly?

zackteo14:05:16

Had a sample html file running a small leaflet script, tried the tile later there

p-himik14:05:55

And judging by the bigger image, it's almost certainly a problem with tile coordinates. I would try reproducing it without react-leaflet.

p-himik14:05:16

Also check browser's JS console for any errors/warnings, just in case.

zackteo14:05:22

Now that you mention it ... Maybe I'm somehow missing some css DevTools failed to load SourceMap: Could not parse content for : Unexpected token < in JSON at position 0

p-himik14:05:11

It's talking about source maps for the CSS, not the CSS itself. That message is harmless.

zackteo14:05:46

Right right :x will try it without react-leaflet then

zackteo22:05:42

Not sure if I should try using react-leaflet via [cljsjs/react-leaflet "2.4.0-0"] instead of via npm

p-himik22:05:37

No difference, but NPM is better because the versions are generally more recent and there can be no issues with pregenerated externs.

p-himik22:05:22

And it also works with the rest of the NPM ecosystem if there are common dependencies. When mixing cljsjs and NPM, you can easily end up with duplicated dependencies in your bundle.

p-himik22:05:28

Long story short, don't use cljsjs.

p-himik22:05:58

If you still have a problem and have no idea what's causing it, try getting rid of stuff. Get rid of as much code as possible. Then of any wrappers of that library. Then of Reagent. Then of ClojureScript. Then of React. If it still doesn't work, you should end up with a minimal reproducible example on your hand that you can directly post in a new issue on their GitHub.

rgm20:05:11

I’m a bit late to this party but we put out our example reagent/re-frame leaflet component here https://github.com/opengb/spork/blob/master/src/opengb/spork/leaflet.cljs … it’s not the most efficient thing in the world (it blows away all the markers every time, for example) but hopefully it’s enough to get started.

Joni Hiltunen19:05:35

I have a weird problem... If I uncomment this line that conditionally disables a button, I can't change the text inputs anymore? Am I doing something wrong o_O https://gist.github.com/Sose/5d4f6a9941295fd6e065dafa4a4172d9#file-login-cljs-L52

p-himik19:05:24

login-panel is a form-1 component. It cannot both use and define ratoms within itself. If you define a ratom within a component, you almost always want to use a form-2 or a form-3 component. Or just reagent.core/with-let .

Joni Hiltunen23:05:14

Re-com related question... Any tips on how I could make the buttons "fill the whole space" of their box container? So that the empty space is "inside the button"... image https://imgur.com/GILRt7F source https://gist.github.com/Sose/06e7234f8daa79ebe60ba17f8220abe7#file-buttons-cljs-L36

Joni Hiltunen00:05:19

If anyone is curious, the answer was to set "width" to "100%" on both the box and the button...

👍 3