Skip to content

Commit baa28ee

Browse files
authored
Merge pull request #32 from koossaayy/feat/html_in_markers
Adding HTML description for markers
2 parents f7b47e2 + 8d9a66b commit baa28ee

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,25 @@ To add markers to your map, you can use the `:markers` attribute, as follows :
116116
```
117117

118118
The `:markers` attribute accepts an array of arrays, each array must have at least the `long` and `lat` keys.
119-
If you want to add a popup description, you may use the `description` key.
119+
If you want to add a popup description, you may use the `description` key. If you want to enable HTML description, you may add `html` key and set it to true to enable it. However there is a catch, you can't directly pass the HTML string, you should encapsulate it in a PHP variable and pass the variable to the array.
120+
For example:
121+
122+
```php
123+
//Somewhere in your code
124+
$htmlString = '<p>Hello world</p>';
125+
```
126+
127+
```html
128+
<x-mapbox
129+
id="mapId"
130+
:markers="[['lat' => 8, 'long' => 10, 'description' => $htmlString, 'html' => true], ['lat'=> 9, 'long' => 10]]"
131+
/>
132+
```
133+
134+
If the array is missing the `html` key, it won't render the passed variable, so make sure to pass it if you want to render an HTML description.
135+
136+
> **Note**
137+
> Please notice that `description` key accepts HTML, and it will render it, so if you are getting your data from your users, please make sure to sanitize it before using it.
120138
121139
Also you can customize the marker icons, instead of using the default ones provided by Mapbox.
122140
To do so you can add `icon` key to the array of markers as follows:

resources/views/includes/script.blade.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@
5454
markerInLoop{{ $id }}.setLngLat([{{ $marker['long'] }}, {{ $marker['lat'] }}]);
5555
5656
@isset($marker['description'])
57-
58-
markerInLoop{{ $id }}.setPopup(new mapboxgl.Popup({
59-
offset: 25
60-
}).setText('{{ $marker['description'] }}'))
57+
@if (isset($marker['html']) && $marker['html'])
58+
markerInLoop{{ $id }}.setPopup(new mapboxgl.Popup({
59+
offset: 25
60+
}).setHTML(`{!! $marker['description'] !!}`))
61+
@else
62+
markerInLoop{{ $id }}.setPopup(new mapboxgl.Popup({
63+
offset: 25
64+
}).setText(`{{ $marker['description'] }}`))
65+
@endif
6166
@endisset
6267
6368
markerInLoop{{ $id }}.addTo(map{{ $id }});

tests/MapboxRenderingTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@
121121
$view->assertSee('map4');
122122
$view->assertSee('map5');
123123
});
124+
125+
it('can render html description', function () {
126+
$html = <<<HTML
127+
<div class="card">
128+
<p>Hello World</p>
129+
</div>
130+
HTML;
131+
132+
$view = $this->component(Mapbox::class, ['id' => 'map', 'markers' => [
133+
['lat' => '1', 'long' => '1', 'icon' => '<img src="https://placekitten.com/g/50/50" style="border-radius: 50%" />', 'html' => true, 'description' => $html],
134+
]]);
135+
136+
$view->assertSee('Hello World');
137+
});

0 commit comments

Comments
 (0)