Skip to content

Instantly share code, notes, and snippets.

@tlux
tlux / ikea-bilresa-scroll-button.yaml
Last active May 2, 2026 21:41
IKEA BILRESA Scroll Button Home Assistant Blueprint
blueprint:
name: IKEA BILRESA Scroll Button Light Control
description: Control a light with an IKEA BILRESA Scroll Button using on/off and level (scroll) commands.
domain: automation
input:
zha_device:
name: Button Device
description: The Zigbee device that sends events
selector:
device:
@tlux
tlux / ikea-e2490-bilresa-scroll-wheel.yaml
Last active May 2, 2026 20:54 — forked from mcinnes01/ikea-e2490-bilresa-scroll-wheel.yaml
IKEA E2490 BILRESA Scroll Wheel (ZHA Integration)
blueprint:
name: IKEA E2490 BILRESA Scroll Wheel (ZHA)
description: |
Unified controller blueprint for IKEA E2490 BILRESA scroll wheel working with ZHA.
- Buttons: on, off, on_double, off_double
- Scroll: brightness_move_to_level with action_level from ZHA move_to_level args
Supports light brightness, media_player volume, light color_temp, light hue.
Version: 2026-05-02
domain: automation
@tlux
tlux / usePreferredColorScheme.ts
Created December 14, 2025 16:04
Hook to detect the preferred color scheme from the user agent
'use client';
import { useEffect, useState } from 'react';
type ColorScheme = 'light' | 'dark';
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
function getPreferredColorScheme(matches: boolean): ColorScheme {
return matches ? 'dark' : 'light';
@tlux
tlux / portal.tsx
Last active August 6, 2025 19:57
React Portal that is mounted as soon as the target element appears in the DOM
import { type PropsWithChildren, useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
type PortalProps = PropsWithChildren & { targetId: string };
export function Portal({ children, targetId }: PortalProps) {
const [targetElement, setTargetElement] = useState<HTMLElement | null>(null);
useEffect(() => {
const updateTargetElement = () => {
@tlux
tlux / useDistinct.ts
Created May 20, 2025 08:45
Hook that checks if a passed value has changed based on the provided equality function and returns the old instance of the * value if it has not changed
import { isEqual } from 'lodash-es';
import { useEffect, useRef, useState } from 'react';
/**
* Checks if a passed value has changed based on the provided equality function and returns the old instance of the
* value if it has not changed.
*
* @param value The value that is compared.
* @param equalFn A function that compares the old and the new value. Defaults to `lodash.isEqual`.
*/
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.time.Duration
@tlux
tlux / use-has-overflow.ts
Created January 3, 2025 18:05
React hook to detect overflow on a ref
import { type RefObject, useEffect, useState } from 'react';
function checkOverflow(el: HTMLElement | null): boolean {
if (!el) return false;
return el.offsetHeight < el.scrollHeight || el.offsetWidth < el.scrollWidth;
}
export function useHasOverflow(ref: RefObject<HTMLElement | null>) {
const [overflow, setOverflow] = useState(false);
@tlux
tlux / use-file.ts
Created December 17, 2024 20:46
React hook to trigger file selection dialog
import { useCallback, useEffect, useRef, useState } from 'react';
function wrapArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}
type UseFileOnSelectCallbackArg<TMultiple extends boolean | undefined> =
TMultiple extends true ? FileList : File;
type UseFileOnSelectCallback<TMultiple extends boolean | undefined = false> = (
@tlux
tlux / use-download.ts
Created December 17, 2024 20:26
React hook to trigger a download of a Blob/File or MediaSource
import { useCallback } from 'react';
export function useDownload() {
return useCallback((obj: Blob | MediaSource, filename: string) => {
const url = URL.createObjectURL(obj);
const link = document.createElement('a');
link.href = url;
if (filename) {
@tlux
tlux / fix-nix.sh
Created November 27, 2024 14:05
Fix Nix after Mac OS update
#!/bin/bash
read -r -d '' INJECT << EOM
# Nix
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
. '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'
fi
# End Nix
EOM