-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useInlineStyle to return CSSProperties to multiple elements
useInlineStyle style property now returns object with keys set as in stylingFunction, this allows multiple elements to be styled if they depend on the same events in referenced element BREAKING CHANGE: style object is not containing CSSProperties but is a Record<string, CSSProperties>
- Loading branch information
1 parent
41d3a33
commit f6dce84
Showing
5 changed files
with
221 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react'; | ||
import { StylingFn } from '..'; | ||
import { useInlineStyle } from '../useInlineStyle'; | ||
|
||
interface TestProps { | ||
isMobile: boolean; | ||
} | ||
|
||
const stylingFn: StylingFn<TestProps> = (state, props) => { | ||
return { | ||
clicker: { | ||
color: state.hover ? 'red' : 'black', | ||
borderColor: state.focus ? 'red' : 'black', | ||
backgroundColor: state.active ? 'red' : 'black', | ||
cursor: 'pointer', | ||
width: props.isMobile ? '90%' : '200px', | ||
}, | ||
}; | ||
}; | ||
|
||
export function BasicExample(props: TestProps): React.ReactElement { | ||
const [ref, style] = useInlineStyle<HTMLDivElement, TestProps>( | ||
stylingFn, | ||
props | ||
); | ||
return ( | ||
<div ref={ref} data-testid='clicker' style={style.clicker}> | ||
test component | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as React from 'react'; | ||
import { StyleState } from '..'; | ||
import { useInlineStyle } from '../useInlineStyle'; | ||
|
||
interface TestProps { | ||
label: boolean; | ||
} | ||
|
||
function stylingFn( | ||
{ focus, hover, active }: Partial<StyleState>, | ||
{ label }: TestProps | ||
): Record<string, React.CSSProperties> { | ||
return { | ||
wrapper: { | ||
backgroundColor: hover ? 'white' : 'blue', | ||
borderColor: active ? 'red' : 'white', | ||
}, | ||
label: { | ||
position: 'absolute', | ||
left: label ? '12px' : 0, | ||
fontSize: '0.75rem', | ||
}, | ||
input: { | ||
backgroundColor: 'white !important', | ||
margin: label ? '20px 0px 32px 0px' : '0px 0px 32px 0px', | ||
padding: '12px 12px', | ||
fontSize: '0.85rem', | ||
border: '1px solid', | ||
borderRadius: '6px', | ||
borderColor: focus ? '#212121' : hover ? '#666' : '#aaa', | ||
boxShadow: focus ? 'inset 0px 0px 2px #212121' : undefined, | ||
}, | ||
error: { | ||
position: 'absolute', | ||
bottom: '12px', | ||
paddingLeft: '12px', | ||
left: '0px', | ||
borderBottom: '1px solid #d30000', | ||
borderLeft: '1px solid #d30000', | ||
color: '#d30000', | ||
fontSize: '0.75rem', | ||
lineHeight: '1.0rem', | ||
}, | ||
}; | ||
} | ||
export function ComplexExample(props: TestProps): React.ReactElement { | ||
const [ref, style] = useInlineStyle<HTMLInputElement, TestProps>( | ||
stylingFn, | ||
props | ||
); | ||
return ( | ||
<div data-testid='wrapper' style={style.wrapper}> | ||
{props.label && ( | ||
<label data-testid='label' style={style.label} htmlFor='field-text'> | ||
label | ||
</label> | ||
)} | ||
<input | ||
data-testid='input' | ||
ref={ref} | ||
style={style.input} | ||
type='text' | ||
id='field-text' | ||
/> | ||
<span data-testid='error' style={style.error}> | ||
error | ||
</span> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters