-
Notifications
You must be signed in to change notification settings - Fork 455
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 #714 from shoutem/release/5.1.0
Release/5.1.0 - master
- Loading branch information
Showing
10 changed files
with
2,351 additions
and
2,201 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,6 @@ | ||
export { | ||
ThemeVariableResolver, | ||
defaultResolver, | ||
resolveVariable, | ||
createScopedResolver, | ||
} from './variableResolver'; |
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,73 @@ | ||
import _ from 'lodash'; | ||
import autoBind from 'auto-bind'; | ||
|
||
const THEME_EXTENSION_SCOPE = 'shoutem.theme'; | ||
|
||
export class ThemeVariableResolver { | ||
constructor(variables) { | ||
this.variables = { ...variables }; | ||
|
||
autoBind(this); | ||
} | ||
|
||
setVariables(variables) { | ||
this.variables = { ...variables }; | ||
} | ||
|
||
getScopedVariable(scope, variable) { | ||
if (!this.variables || _.isEmpty(this.variables)) { | ||
throw new Error( | ||
'Theme variable resolver not yet initialised. Please call `setVariables` prior to this function call', | ||
); | ||
} | ||
|
||
const scopePath = _.split(scope, '.'); | ||
const variablePath = _.split(variable, '.'); | ||
|
||
return _.get( | ||
this.variables, | ||
[...scopePath, ...variablePath], | ||
_.get(this.variables, variablePath) | ||
); | ||
} | ||
|
||
// Currently supports 2 function signatures | ||
// resolveVariable(variableName) -> Tries to get the variable under the root scope ( generic theme values ) | ||
// resolveVariable(scope, variableName) -> Tries to get the variable under specific scope | ||
// Variable name can be stringified object path or a regular string, i.e -> text.color | ||
resolveVariable(...params) { | ||
if (arguments.length === 1) { | ||
const variablePath = _.split(params[0], '.'); | ||
|
||
return _.get(this.variables, variablePath); | ||
} | ||
|
||
if (arguments.length === 2) { | ||
return this.getScopedVariable(params[0], params[1]); | ||
} | ||
|
||
throw new Error( | ||
"Invalid theme variable resolution call. Please use full signature \n-> resolveVariable(scope, variableName),\n or the shorthand if you're getting a variable from shoutem.theme extension \n->resolveVariable(variableName)", | ||
); | ||
} | ||
|
||
createScopedResolver(scope) { | ||
return (...params) => { | ||
if (params.length === 1) { | ||
return this.getScopedVariable(scope, params[0]); | ||
} | ||
if (params.length === 2) { | ||
return this.getScopedVariable(params[0], params[1]); | ||
} | ||
|
||
return undefined; | ||
}; | ||
} | ||
} | ||
|
||
// Expose both, the resolver service, and the default resolver working with | ||
// shoutem default theme. This way the logic can be used for multiple themes/resolvers, or | ||
// if needed, you can override the default theme variables using the setVariables method | ||
export const defaultResolver = new ThemeVariableResolver(); | ||
export const resolveVariable = defaultResolver.resolveVariable; | ||
export const createScopedResolver = defaultResolver.createScopedResolver; |
Oops, something went wrong.