mapbox / mapbox/mapbox-gl-js

Allow expressions as elements in array output values

Open
#6,155 27 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature :green_apple:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

mapbox-gl-js version: v0.44.0

It would be great if I could use expressions in each element of an array value for properties like text-font and *-translate.

Lets take text-offset for example. The output value is a 2-element array. As a literal, can be written like this:

[0,1]

Lets imagine a hypothetical situation where I wanted to offset along the y axis based on the existence of a property in my data. This would be a useful feature if I had icons for some labels but not for others. I can write this expression like so, and it's valid:

[
  "case",
  ["has", "icon"],
  ["literal", [0, -10]],
  ["literal", [0, 0]]
]

If I have more than one case where I want to adjust my y axis value (say, to check for the existence of 'big-icon' and 'small-icon'), the syntax gets even more unwieldy:

[
  "case",
  ["has", "icon-small"],
  ["literal", [0, -10]],
  ["has", "icon-large"],
  ["literal", [0, -20]],
  ["literal", [0, 0]]
]

It would be amazing if the following was valid:

[
  0,
  [ "case", 
    ["has", "icon-small"],
    -10
    ["has", "icon-large"],
    -20,
    0
  ]
]

Now, that probably won't work because of the way the expression syntax is designed. Could a to-array expression get me what I want?

[
  "to-array",
  0,
  [ "case", ["has", "icon"], -10, 0]
]

This would be a really valuable feature for many properties, and particularly text-font, where usually users will have a universal fallback font, but would want to use an expression to dictate what the primary font is. It would also lead to a more user-friendly experience in Studio!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the expression syntax and the handling of array-valued properties such as text-offset, text-font, and *-translate. Compare the existing literal-array and case examples with the proposed element-wise expressions or to-array form. Done means a supported, documented syntax works for these properties and the relevant expression behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.