Skip to content

selection.merge(transition…)? #257

@Fil

Description

@Fil

d3-selection@2 release notes mention:

selection.merge(transition) now throws an error to make it more obvious when you attempt to merge a selection with something that’s not, say because you mistakenly returned a transition from your selection.join’s update function.

However thanks to #248 selection.merge could call transition|selection.selection() before merging.

This would simplify a lot the examples in https://observablehq.com/@d3/selection-join.

svg.selectAll("text")
      .data(randomLetters(), d => d)
      .join(
        enter => enter.append("text")
            .attr("fill", "green")
            .attr("x", (d, i) => i * 16)
            .attr("y", -30)
            .text(d => d)
          .transition(t)
            .attr("y", 0), // 🧨
        update => update
            .attr("fill", "black")
            .attr("y", 0)
          .transition(t)
            .attr("x", (d, i) => i * 16), // 🧨
        exit => exit
            .attr("fill", "brown")
           .transition(t)
            .attr("y", 30)
            .remove()
      );

Note that we can already do:

svg.selectAll("text")
      .data(randomLetters(), d => d)
      .join(
        enter => enter.append("text")
            .attr("fill", "green")
            .attr("x", (d, i) => i * 16)
            .attr("y", -30)
            .text(d => d)
          .transition(t)
            .attr("y", 0)
          .selection(), // 🤘
        update => update
            .attr("fill", "black")
            .attr("y", 0)
          .transition(t)
            .attr("x", (d, i) => i * 16)
          .selection(), // 🤘
        exit => exit
            .attr("fill", "brown")
           .transition(t)
            .attr("y", 30)
            .remove()
      );

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions