Skip to content

Compose and memoize a list of non-falsy space-separated CSS class names from an array, or yields of a generator function.

License

Notifications You must be signed in to change notification settings

rapid-platform/use-composed-class-name

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Hook — useComposedClassName

Compose and memoize a list of non-falsy space-separated CSS class names from an array, or yields of a generator function.

This library has no dependencies.

function useComposedClassName(classNameGenerator: string[] | (() => Generator<string, void, unknown>), deps?: React.DependencyList): string;

Examples

JS Generator function

// returns "btn btn-primary" for non-active states and "btn btn-primary btn-active"
// for active states
const [active, setActive] = useState(false);
const className = useComposedClassName(function*() {
    yield 'btn';
    yield 'btn-primary';
    if (active) {
        yield 'btn-active';
    }
}, [active]);

Truthy items in an array

// returns "btn btn-primary" for non-active states and "btn btn-primary btn-active"
// for active states
const [active, setActive] = useState(false);
const className = useComposedClassName([
    'btn',
    'btn-primary',
    active && 'btn-active',
], [active]);

About

Compose and memoize a list of non-falsy space-separated CSS class names from an array, or yields of a generator function.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published