legend
StableSays what the colours and symbols of the map mean, in a corner of the map, and hides and shows layers from its entries.
position
Set assigns
Press Parks or Railways in the legend to hide or show them. With collapsible, its title folds it away.
Sample data: the stations, the depot and the districts' noise are made up; parks and railways are OpenStreetMap's, in OpenFreeMap's tiles.
- @position
- top-left
- @collapsible
- false
- @toggled
- nil
<div id="legend-example">
<.example_frame error={@map_error} error_id="example-legend-map-error">
<.map
id="example-legend-map"
center={[-3.7038, 40.4168]}
zoom={12.2}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- OpenFreeMap's TileJSON: OpenStreetMap in the OpenMapTiles schema. --%>
<.vector_tile_source
id="example-legend-osm"
map_id="example-legend-map"
url="https://tiles.openfreemap.org/planet"
events={["source:error"]}
/>
<.fill_layer
id="example-legend-parks"
map_id="example-legend-map"
source_id="example-legend-osm"
source_layer="park"
paint={%{"fill-color" => parks(), "fill-opacity" => 0.45}}
/>
<.line_layer
id="example-legend-rail"
map_id="example-legend-map"
source_id="example-legend-osm"
source_layer="transportation"
filter={["==", ["get", "class"], "rail"]}
paint={%{"line-color" => rail(), "line-width" => 2}}
/>
<%!-- The districts' fill and the stations' colour read the palettes the
legend reads too. --%>
<.geojson_layer
id="example-legend-districts"
map_id="example-legend-map"
type="fill"
data={@districts}
paint={%{"fill-color" => noise_color(), "fill-opacity" => 0.3}}
/>
<.geojson_layer
id="example-legend-stations"
map_id="example-legend-map"
type="circle"
data={@stations}
paint={
%{
"circle-color" => kind_color(),
"circle-radius" => 6,
"circle-stroke-color" => "#ffffff",
"circle-stroke-width" => 1.5
}
}
/>
<.marker id="example-legend-depot" map_id="example-legend-map" lng_lat={@depot}>
<.depot_icon />
</.marker>
<%!-- The legend, rendered by the server: a square and a line that hide
and show their layers in the browser, a circle for each kind of
station, the depot drawn by its own icon, and the noise's scale. --%>
<.legend
id="example-legend"
map_id="example-legend-map"
position={@position}
title={gettext("Madrid, sample map")}
collapsible={@collapsible}
events={["legend:layer_toggled"]}
>
<:item color={parks()} label={gettext("Parks")} layers={["example-legend-parks"]} />
<:item
color={rail()}
shape={:line}
label={gettext("Railways")}
layers={["example-legend-rail"]}
/>
<:item :for={{kind, color} <- kinds()} color={color} shape={:circle} label={kind_label(kind)} />
<:item label={gettext("Depot")}><.depot_icon class="size-4" /></:item>
<:gradient
colors={noise_colors()}
from={elem(noise_range(), 0)}
to={elem(noise_range(), 1)}
label={gettext("Noise")}
/>
</.legend>
<div class="space-y-3 border-t border-line px-4 py-4">
<div class="grid gap-x-8 gap-y-4 lg:grid-cols-[auto_1fr]">
<%!-- position: the corner of the map; a new value moves the legend. --%>
<.example_group id="example-legend-position" label="position" code>
<.example_button
:for={position <- positions()}
id={"example-legend-position-#{position}"}
code
pressed={@position == position}
phx-click="position"
phx-value-position={position}
>
{position}
</.example_button>
</.example_group>
<.example_group id="example-legend-assigns" label={gettext("Set assigns")}>
<.form for={@options} id="example-legend-options" phx-change="options" class="pt-1">
<.example_checkbox field={@options[:collapsible]} label="collapsible" />
</.form>
</.example_group>
</div>
<.example_note id="example-legend-hint" icon="hero-cursor-arrow-rays-micro">
{gettext(
"Press Parks or Railways in the legend to hide or show them. With collapsible, its title folds it away."
)}
</.example_note>
<.example_note id="example-legend-sample-data">
{gettext(
"Sample data: the stations, the depot and the districts' noise are made up; parks and railways are OpenStreetMap's, in OpenFreeMap's tiles."
)}
</.example_note>
</div>
<%!-- legend:layer_toggled sets toggled. --%>
<.example_readout values={[
position: @position,
collapsible: @collapsible,
toggled: @toggled || "nil"
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the legend. |
| map_id required | string | — | ID of the map to put the legend on. |
| position | string |
"bottom-left"
|
The corner of the map: "top-left", "top-right", "bottom-left" or "bottom-right". Defaults to "bottom-left". A new value moves the legend to that corner.
|
| title | string |
nil
|
The legend's heading, an <h2> that names its list of entries. Optional.
|
| collapsible | boolean |
false
|
Whether the title is a button that collapses and opens the legend, in the browser (see "Collapsing" above). Defaults to false. It needs a title.
|
| events | any |
[]
|
a list of event names, or :all
|
| global attributes | global | — | Any other HTML attribute, passed on to the component's element. |
Slots
- item Optional.
- gradient Optional.