@@ -6,8 +6,18 @@ const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
66
77const ALLOWED_NODE_ENVS = [ 'dev' , 'development' ]
88
9+ export interface UseLog {
10+ styles ?: {
11+ componentCSS ?: string
12+ changeCSS ?: string
13+ subValueCSS ?: string
14+ }
15+ }
16+
17+ export type Log = UseLog
18+
919export interface UseLogReturn {
10- log : < T > ( value : T ) => void
20+ log : < T > ( value : T , props ?: Log ) => void
1121}
1222
1323interface PrintProps < T > {
@@ -24,7 +34,13 @@ export enum PrintTypes {
2434 Change = 'Change' ,
2535}
2636
27- export function useLog ( ) : UseLogReturn {
37+ export function useLog ( {
38+ styles : {
39+ componentCSS = CSS_COMPONENT ,
40+ changeCSS = CSS_CHANGE ,
41+ subValueCSS = CSS_SUB_VALUE ,
42+ } = { } ,
43+ } : UseLog = { } ) : UseLogReturn {
2844 const componentName =
2945 ( function getComponentName ( ) {
3046 try {
@@ -48,31 +64,38 @@ export function useLog(): UseLogReturn {
4864 } %c@ ${ new Date ( ) . toLocaleTimeString ( ) } `
4965 }
5066
51- function print < T > ( {
52- value,
53- label ,
54- prevValue ,
55- type = PrintTypes . Change ,
56- group = getGroupLabel ( type ) ,
57- } : PrintProps < T > ) : void {
58- console . group ( group , CSS_COMPONENT , CSS_SUB_VALUE )
59-
60- if ( ! ( 'prevValue' in arguments [ 0 ] ) ) {
61- console . log ( ` ${ label . padStart ( 14 , ' ' ) } : ${ String ( value ) } ` )
62- } else {
63- console . log (
64- `Previous value: %c ${ String ( arguments [ 0 ] . prevValue ) } ` ,
65- CSS_SUB_VALUE ,
67+ function log < T > ( value : T , props ?: Log ) : void {
68+ const clonedValue = JSON . parse ( JSON . stringify ( value ) )
69+ const prevValueRef = useRef < T > ( )
70+
71+ function print < T > ( {
72+ value ,
73+ label ,
74+ prevValue ,
75+ type = PrintTypes . Change ,
76+ group = getGroupLabel ( type ) ,
77+ } : PrintProps < T > ) : void {
78+ console . group (
79+ group ,
80+ props ?. styles ?. componentCSS ?? componentCSS ,
81+ props ?. styles ?. subValueCSS ?? subValueCSS ,
6682 )
67- console . log ( ` Current value: %c${ String ( value ) } ` , CSS_CHANGE )
68- }
6983
70- console . groupEnd ( )
71- }
84+ if ( ! ( 'prevValue' in arguments [ 0 ] ) ) {
85+ console . log ( `${ label . padStart ( 14 , ' ' ) } : ${ String ( value ) } ` )
86+ } else {
87+ console . log (
88+ `Previous value: %c${ String ( arguments [ 0 ] . prevValue ) } ` ,
89+ props ?. styles ?. subValueCSS ?? subValueCSS ,
90+ )
91+ console . log (
92+ ` Current value: %c${ String ( value ) } ` ,
93+ props ?. styles ?. changeCSS ?? changeCSS ,
94+ )
95+ }
7296
73- function log < T > ( value : T ) : void {
74- const clonedValue = JSON . parse ( JSON . stringify ( value ) )
75- const prevValueRef = useRef < T > ( )
97+ console . groupEnd ( )
98+ }
7699
77100 if ( ALLOWED_NODE_ENVS . includes ( process . env . NODE_ENV ?? '' ) ) {
78101 return ( function logHooks ( ) {
0 commit comments