Skip to content

Commit

Permalink
feat: expose styleState from useInlineStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
stopyransky committed Dec 26, 2021
1 parent 6c76953 commit 73826fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/useClickAway/useClickAway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, useCallback, useRef } from 'react';
import { useEffect, useState, useRef } from 'react';

interface ClickAwayPayload<T> {
ref: React.RefObject<T>;
Expand Down
10 changes: 7 additions & 3 deletions src/useInlineStyle/useInlineStyle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BasicExample } from './examples/BasicExample';
import { ComplexExample } from './examples/ComplexExample';

describe('basic usage of useInlineStyle hook', () => {
it('should return [ref, style] tuple', () => {
it('should return [ref, style, styleState] array', () => {
const { result, unmount } = renderHook(() =>
useInlineStyle(() => ({
testElStyle: {
Expand All @@ -15,12 +15,16 @@ describe('basic usage of useInlineStyle hook', () => {
}))
);

const [ref, style] = result.current;
const [ref, style, state] = result.current;

expect(typeof ref).toBe('object');
expect(typeof style).toBe('object');
expect(style.testElStyle).toBeDefined();
expect(typeof style.testElStyle).toBe('object');
expect(typeof ref).toBe('object');
expect(typeof state).toBe('object');
expect(typeof state.hover).toEqual('boolean');
expect(typeof state.focus).toEqual('boolean');
expect(typeof state.active).toEqual('boolean');
unmount();
});

Expand Down
8 changes: 6 additions & 2 deletions src/useInlineStyle/useInlineStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const subscribeToEvents = (el, setStyle) => {
export function useInlineStyle<T extends HTMLElement, P>(
styleFn: StylingFn<P>,
props?: Partial<P>
): [ref: RefObject<T>, style: Record<string, CSSProperties>] {
): [
ref: RefObject<T>,
style: Record<string, CSSProperties>,
styleState: StyleState
] {
const ref = useRef<T>(null);
const [styleState, setStyle] = useSmartReducer(initialState);

Expand All @@ -60,5 +64,5 @@ export function useInlineStyle<T extends HTMLElement, P>(

useEffect(() => subscribeToEvents(ref.current, setStyle), []);

return [ref, style];
return [ref, style, styleState];
}

0 comments on commit 73826fc

Please sign in to comment.