Skip to content

Allow attachments to return non-void #16660

@janosh

Description

@janosh

Describe the problem

maybe this should be a discussion. if so, feel free to convert.

this Svelte attachment picks black or white text color to maximize contrast with node's background color:

export function contrast_color(
  text_color_threshold: number = 0.7,
  choices: [string, string] = [`black`, `white`], // one dark and one light color (in that order)
) {
  return (node: HTMLElement | null) => {
    const light_bg = luminance(get_bg_color(node)) > text_color_threshold
    const text_color = light_bg ? choices[0] : choices[1] // white text for dark backgrounds, black for light
    if (node) node.style.color = text_color
    return text_color
  }
}

in some cases, i'd like to call it outside a Svelte component and have it return the text color it would apply to the node:

const text_color = contrast_color(0.6)(null)

unfortunately, returning the color string from the attachment raises a type error when applied to a Svelte component:

Type string is not assignable to type void | (() => void)

Image

Describe the proposed solution

maybe the allowed attachment return type could be broadened to enable this?

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions