Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: IsFocusWithin #103

Merged
merged 13 commits into from
Jul 1, 2024
Merged

feat: IsFocusWithin #103

merged 13 commits into from
Jul 1, 2024

Conversation

huntabyte
Copy link
Member

@huntabyte huntabyte commented Jun 30, 2024

Tracks whether focus is within a given target element.

<script lang="ts">
	import { IsFocusWithin } from 'runed'

	let formElement = $state<HTMLElement | undefined>()
	const focusWithinForm = new IsFocusWithin(() => formElement)
</script>

<span>Focus is within: {focusWithinForm.current}</span>
<form bind:this={formElement}>
	<input />
</form>

I also added testing library so we can test components.

Copy link

changeset-bot bot commented Jun 30, 2024

🦋 Changeset detected

Latest commit: 5b6b2bb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
runed Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Jun 30, 2024

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview 5b6b2bb

@abdel-17
Copy link
Collaborator

I feel like this might encourage relying on JS for styling instead of just the :focus-within selector.

@huntabyte
Copy link
Member Author

huntabyte commented Jun 30, 2024

I feel like this might encourage relying on JS for styling instead of just the :focus-within selector.

The same could be said for the activeElement instead of :focus. The intentions aren't for it to be used for styling but rather side effects when focus is/isn't within a specific element which is useful.

If someone really wants to instantiate a class, bind to the reference of an element, and conditionally style something vs using the CSS selector then they have problems we're incapable of solving.

@huntabyte
Copy link
Member Author

Also, there is a use case for styling where you want to style something that isn't the form element differently when the form element has focus within.

@abdel-17
Copy link
Collaborator

abdel-17 commented Jun 30, 2024

Also, there is a use case for styling where you want to style something that isn't the form element differently when the form element has focus within.

You can still use CSS for that.

Style a child
form:focus-within .some-selector

Style a sibling
form:focus-within + .some-selector

Style a parent
.some-selector:has(form:focus-within)

But yeah I get the other use case.

@huntabyte huntabyte merged commit 6353651 into main Jul 1, 2024
4 checks passed
@huntabyte huntabyte deleted the feat/is-focus-within branch July 1, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants