This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
forked from Semantic-Org/Semantic-UI-React
-
Notifications
You must be signed in to change notification settings - Fork 2
docs(examples): show only relevant styling variables #80
Open
kuzhelov
wants to merge
2
commits into
master
Choose a base branch
from
feat/docs-show-only-relevant-variables
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,81 @@ | ||
export type WithTrackedVariables<TProps> = TProps extends { variables: infer TVariables } | ||
? TProps & { trackVariables: () => TVariables } | ||
: never | ||
|
||
type VariableTouchedCallback = (variableName: string | number | symbol) => void | ||
|
||
interface VariableSpy { | ||
onVariableTouched: VariableTouchedCallback | ||
whenApplied: () => void | ||
} | ||
|
||
interface PerComponentVariableSpies { | ||
[componentDisplayName: string]: VariableSpy | ||
} | ||
|
||
interface VariableSpySpec { | ||
componentDisplayName: string | ||
onVariableTouched: VariableTouchedCallback | ||
whenApplied?: () => void | ||
} | ||
|
||
const DoNothing = () => {} | ||
|
||
export function createSpy(spec: VariableSpySpec): PerComponentVariableSpies { | ||
const { componentDisplayName, onVariableTouched, whenApplied } = spec | ||
|
||
return { | ||
[componentDisplayName]: { | ||
onVariableTouched, | ||
whenApplied: whenApplied || DoNothing, | ||
}, | ||
} | ||
} | ||
|
||
function createTrackedVariablesProvider<TVariables extends object>( | ||
spy: VariableSpy, | ||
variables: TVariables, | ||
): () => TVariables { | ||
return () => { | ||
spy.whenApplied() | ||
|
||
return new Proxy(variables, { | ||
get: (it, variableName) => { | ||
spy.onVariableTouched(variableName) | ||
return it[variableName] | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export function connect<TVariables extends object>( | ||
spies: PerComponentVariableSpies, | ||
componentDisplayName: string, | ||
variables: TVariables, | ||
): () => TVariables { | ||
if (!spies || !spies[componentDisplayName]) { | ||
return () => variables | ||
} | ||
|
||
return createTrackedVariablesProvider(spies[componentDisplayName], variables) | ||
} | ||
|
||
export class ActiveVariablesTracker { | ||
isEnabled = false | ||
activeVariables: (string | number | symbol)[] = [] | ||
|
||
registerAsActive(variableName: string | number | symbol): void { | ||
if (!this.activeVariables.some(v => v === variableName)) { | ||
this.activeVariables.push(variableName) | ||
} | ||
} | ||
|
||
isActive(variableName: string | number | symbol): boolean { | ||
return this.activeVariables.some(v => v === variableName) | ||
} | ||
|
||
resetAndDisable(): void { | ||
this.isEnabled = false | ||
this.activeVariables = [] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to not have this part of the public API at present. One thought is to first consume the theme in the tree, clone it and observe it at that point, then provide the observed version down stream.
This could be wrapped up in a new component something like this (assumes some
observe()
deep clone exists):Now, anywhere we'd like to observe context in the tree we can use it like so:
Now, any rules rendered down stream of our
ProviderObserver
that access the variables should result in firing theonGet
callback. Here, we can track those variables.What's also neat about this is that we can now also tell any part of the theme is accessed. Including listing out any other components that may be at play in the example.
Let's see if we can get an implementation working that does not require changes to any existing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your feedback - agree, will look into that