-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from vtex/feature/profile-form-css-handles
Feature/profile form css handles
- Loading branch information
Showing
9 changed files
with
157 additions
and
15 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import { useCssHandles } from 'vtex.css-handles' | ||
import ProfileField from './ProfileField' | ||
|
||
function ProfileFieldWrapper(props) { | ||
const handles = useCssHandles([props.cssHandle]) | ||
|
||
return ( | ||
<div className={handles[props.cssHandle]}> | ||
<ProfileField {...props} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default ProfileFieldWrapper |
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,117 @@ | ||
/* eslint-disable default-param-last */ | ||
import React, { createContext, useContext, useMemo } from 'react' | ||
|
||
export const useCssHandles = cssHandles => { | ||
const handles = {} | ||
|
||
cssHandles.forEach(handle => { | ||
handles[handle] = handle | ||
}) | ||
|
||
return { handles, withModifiers: withModifiersHelper(handles) } | ||
} | ||
|
||
const withModifiersHelper = handles => (handle, modifier) => { | ||
return applyModifiers(handles[handle], modifier) | ||
} | ||
|
||
export const createCssHandlesContext = cssHandles => { | ||
const Context = createContext({ | ||
handles: cssHandles, | ||
withModifiers: withModifiersHelper(cssHandles), | ||
}) | ||
|
||
const useContextCssHandles = () => { | ||
return useContext(Context) | ||
} | ||
|
||
const CssHandlesProvider = ({ withModifiers, handles, children }) => { | ||
const value = useMemo( | ||
() => ({ | ||
handles, | ||
withModifiers, | ||
}), | ||
[withModifiers, handles] | ||
) | ||
|
||
return <Context.Provider value={value}>{children}</Context.Provider> | ||
} | ||
|
||
return { useContextCssHandles, CssHandlesProvider } | ||
} | ||
|
||
const validateModifier = modifier => { | ||
if (typeof modifier !== 'string') { | ||
console.error( | ||
`Invalid modifier type on \`cssHandles.applyModifier\`. All modifiers should be strings, found "${modifier}" ` | ||
) | ||
|
||
return false | ||
} | ||
|
||
/* This is not an error, so doesn't log any message, but should | ||
* invalidate the current modifier and not include it */ | ||
if (modifier === '') { | ||
return false | ||
} | ||
|
||
if (/[^A-z0-9-]/.test(modifier)) { | ||
console.error( | ||
`Invalid modifier on \`cssHandles.applyModifier\`. Modifiers should contain only letters, numbers or -` | ||
) | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
export const applyModifiers = (handles, modifier) => { | ||
const normalizedModifiers = | ||
typeof modifier === 'string' ? [modifier] : modifier | ||
|
||
if (!Array.isArray(normalizedModifiers)) { | ||
console.error( | ||
'Invalid modifier type on `cssHandles.applyModifier`. Please use either a string or an array of strings' | ||
) | ||
|
||
return handles | ||
} | ||
|
||
const splitHandles = handles.split(' ') | ||
|
||
const modifiedHandles = normalizedModifiers | ||
.map(currentModifier => { | ||
const isValid = validateModifier(currentModifier) | ||
|
||
if (!isValid) { | ||
return '' | ||
} | ||
|
||
return splitHandles | ||
.map(handle => `${handle}--${currentModifier}`) | ||
.join(' ') | ||
.trim() | ||
}) | ||
.filter(l => l.length > 0) | ||
.join(' ') | ||
.trim() | ||
|
||
return splitHandles.concat(modifiedHandles).join(' ').trim() | ||
} | ||
|
||
export const withCssHandles = | ||
(handles = [], options) => | ||
Component => { | ||
const EnhancedComponent = props => { | ||
const { handles: cssHandles } = useCssHandles(handles, options) | ||
|
||
return <Component handles={cssHandles} {...props} /> | ||
} | ||
|
||
const displayName = Component.displayName || Component.name || 'Component' | ||
|
||
EnhancedComponent.displayName = `withCssHandles(${displayName})` | ||
|
||
return EnhancedComponent | ||
} |
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
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 |
---|---|---|
|
@@ -92,7 +92,8 @@ | |
"nwb": "^0.21.5", | ||
"react-dom": "^16.4.1", | ||
"react-hot-loader": "^4.3.4", | ||
"react-test-renderer": "^16.4.1" | ||
"react-test-renderer": "^16.4.1", | ||
"vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles" | ||
}, | ||
"peerDependencies": { | ||
"vtex-tachyons": "^2.5.0" | ||
|
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 |
---|---|---|
|
@@ -11276,6 +11276,10 @@ vtex-tachyons@^2.6.0: | |
resolved "https://registry.yarnpkg.com/vtex-tachyons/-/vtex-tachyons-2.10.0.tgz#d72363dcd77d0960cb23efc82e0e8d168714f153" | ||
integrity sha512-WWEOR4iyrMQ2fzp+EActa5ZLaYSMdPk2yxP8XQRhg+q+QnbFH2EKGayjkea4okXRDE7KTKQZdLF3GlB/HaXOow== | ||
|
||
"vtex.css-handles@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles": | ||
version "0.4.4" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles#8c45c6decf9acd2b944e07261686decff93d6422" | ||
|
||
w3c-hr-time@^1.0.1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz#0a89cdf5cc15822df9c360543676963e0cc308cd" | ||
|