Skip to content

Replace select().closest() calls with :has #4456

Closed

Description

@fregante

The :has() CSS selector is nowhere to be seen yet and given the recent de-class-ification of GitHub’s DOM we found ourself needing code like this very often:

select('.some-octicon').closest('details')
"Custom API" suggestion, discarded. Sizzle is best

so I wonder how we can "polyfill" the selector without have to load Sizzle. Example:

select2('details').has('.some-octicon')

Example:

function select2(selector: string) {
	return {
		has(children: string) {
			return select(children)?.closest(selector);
		}
	}
}

select2.all = (selector: string) => {
	return {
		has(children: string) {
			return select.all(children) :filter/map: .closest(selector);
		}
	}
}

Thoughts? Worth it at all?

Activity

  1. added
    meta
    on Jun 10, 2021
  2. fregante commented on Jun 10, 2021

    @fregante
    MemberAuthor

    Or maybe, instead of awkward APIs, we could just load up Sizzle, 20KB

    sizzle('details:has(.some-octicon)')[0].textContent = 'Hi!'
  3. changed the title [-]Replace `select().closest()` calls with custom `select` API[/-] [+]Replace `select().closest()` calls `sizzle`[/+] on Aug 18, 2021
  4. fregante commented on Aug 18, 2021

    @fregante
    MemberAuthor

    The advantage of just using sizzle is that we can drop it easily once :has() becomes available in every browser (2028)

  5. added
    small
    on Aug 24, 2021
  6. changed the title [-]Replace `select().closest()` calls `sizzle`[/-] [+]Replace `select().closest()` calls with `sizzle`[/+] on Aug 24, 2021
  7. kidonng commented on Oct 11, 2021

    @kidonng
    Member

    This will also save some textContent.includes we have, using :contains (not that urgent though).

  8. fregante commented on Oct 11, 2021

    @fregante
    MemberAuthor

    This will also save some textContent.includes we have, using :contains (not that urgent though).

    That'd be nice, but then we won't be able to drop the dependency.

    I think it's still worth it for now though since… we'll probably have the dependency for a while anyway.

  9. fregante commented on Mar 15, 2022

    @fregante
    MemberAuthor

    5% of our users have it. 95% more to go.

    FN3oQM8XoAEqduW

  10. changed the title [-]Replace `select().closest()` calls with `sizzle`[/-] [+]Replace `select().closest()` calls with `:has`[/+] on Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedmetasmall

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions