Skip to content

esc-to-cancel - Restore feature on PRs#9280

Merged
fregante merged 10 commits into
refined-github:mainfrom
anasm266:anasc/restore-esc-to-cancel-pr-header
Apr 30, 2026
Merged

esc-to-cancel - Restore feature on PRs#9280
fregante merged 10 commits into
refined-github:mainfrom
anasm266:anasc/restore-esc-to-cancel-pr-header

Conversation

@anasm266

@anasm266 anasm266 commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #9250

Test URLs

Demo gif

(

fix_tested.mp4

@github-actions github-actions Bot added the bug label Apr 25, 2026
@github-actions github-actions Bot changed the title Fix esc-to-cancel on new PR header Fix esc-to-cancel on new PR header Apr 25, 2026
Comment thread source/features/esc-to-cancel.tsx Outdated

function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement | undefined {
for (const titleContainerSelector of titleContainerSelectors) {
const titleContainer = field.closest(titleContainerSelector);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No loop needed, just pass the whole array to .closest()

Comment thread source/features/esc-to-cancel.tsx Outdated
function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement | undefined {
for (const titleContainerSelector of titleContainerSelectors) {
const titleContainer = field.closest(titleContainerSelector);
if (!titleContainer) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closest() must/will find the element, so no condition needed here

Comment thread source/features/esc-to-cancel.tsx Outdated
continue;
}

const localCancelButton = [...titleContainer.querySelectorAll<HTMLButtonElement>('button:not([disabled])')]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$$() already selects, returns an array, and is properly typed:

Suggested change
const localCancelButton = [...titleContainer.querySelectorAll<HTMLButtonElement>('button:not([disabled])')]
const localCancelButton = $$('button:not([disabled])')

Comment thread source/features/esc-to-cancel.tsx Outdated
}
}

return $optional<HTMLButtonElement | HTMLAnchorElement>('.js-cancel-issue-edit');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be optional. The button must be found. If it's not found, the selectors are wrong and/or GitHub was updated. In both cases we want an error to be thrown ($() will throw, $optional() doesn't)

|| field.hasAttribute('aria-autocomplete')
// Classic autocomplete dropdown
|| elementExists('.suggester', field.form!)
|| elementExists('.suggester', field.form ?? undefined)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't make unnecessary changes

Comment thread source/features/esc-to-cancel.tsx Outdated
}

const cancelButton = findCancelButton(event.delegateTarget);
if (!cancelButton) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned, the cancel button must be found

@fregante

fregante commented Apr 25, 2026

Copy link
Copy Markdown
Member

Note that untested AI PRs will result in a block. So make sure it works and ideally record a screencast or gif (with LICEcap if you use macos for example)

@anasm266

Copy link
Copy Markdown
Contributor Author
fix_tested.mp4

@anasm266 anasm266 requested a review from fregante April 25, 2026 19:41
@anasm266 anasm266 marked this pull request as ready for review April 26, 2026 03:54

@fregante fregante left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks reasonable already

Comment thread source/features/esc-to-cancel.tsx Outdated

function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement {
const titleContainer = field.closest(titleContainerSelectors.join(','))!;
return $$('button:not([disabled])', titleContainer)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without changing the order of operations, we have to temporarily use $$optional here or else it might throw on the old version.

Comment thread source/features/esc-to-cancel.tsx Outdated
Comment thread source/features/esc-to-cancel.tsx Outdated
@SunsetTechuila SunsetTechuila changed the title Fix esc-to-cancel on new PR header esc-to-cancel - Restore feature on PRs Apr 26, 2026
Comment thread source/features/esc-to-cancel.tsx Outdated

function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement {
const titleContainer = field.closest(titleContainerSelectors);
return $$optional('button:not([disabled])', titleContainer ?? undefined)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fregante allow baseElements in select-dom to be null 🙏

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I follow sindresorhus/meta#7

I guess an exception can be made since the DOM has a lot of nulls. PR welcome to explore it further

// Old view -- TODO: Remove after legacy PR files view is removed
'.js-cancel-issue-edit',
]);
if (cancelButton.textContent.trim() !== 'Cancel') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have assertNodeContent

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which works only with text nodes or elements containing only them

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Therefore the code can be adjusted to use it, what's the issue? cancelButton.firstChild probably.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that it would be cancelButton.firstChild.firstChild

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or cancelButton.querySelector('span[data-component="text"]'). I like neither

}

// TODO: Drop in March 2025, implemented by GitHub
// https://github.com/refined-github/refined-github/pull/7892

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still want a TODO comment to check if GitHub is already handling the keypress or not. September 2026 would be good, then every 6 months or so.

Comment thread source/features/esc-to-cancel.tsx Outdated
Co-authored-by: fregante <[email protected]>
@fregante fregante merged commit a49635e into refined-github:main Apr 30, 2026
10 checks passed
Copilot AI pushed a commit that referenced this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

esc-to-cancel broken on new PR view

3 participants