Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(docs): adding theme switcher #280

Merged
merged 31 commits into from
Oct 3, 2018
Merged

feat(docs): adding theme switcher #280

merged 31 commits into from
Oct 3, 2018

Conversation

mnajdova
Copy link
Contributor

@mnajdova mnajdova commented Sep 26, 2018

Docs - theme switcher

This PR is adding the switching theme feature on the docs site. For this to work, the following things are implemented:

  1. Mobx is added as a dependency for storing the currently selected theme (we are storing just the name of the theme here)

  2. Dropdown menu is added on the sidebar, where all exported themes are displayed as options.

  3. Two additional themes are added: teams dark and teams high contrast theme. These themes are simple and are just adding some styling for the button component, so that we can showcase the theme switching.

  4. The selected theme is merged with the teams default theme in the root Provider of the docs.

Demo:

ezgif com-gif-maker

manajdov added 2 commits September 24, 2018 17:55
-added switcher for example // TODO it is merging the both themes
@codecov
Copy link

codecov bot commented Sep 26, 2018

Codecov Report

Merging #280 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #280   +/-   ##
=======================================
  Coverage   89.63%   89.63%           
=======================================
  Files          62       62           
  Lines        1167     1167           
  Branches      173      150   -23     
=======================================
  Hits         1046     1046           
  Misses        119      119           
  Partials        2        2

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d9e8eae...7e4835b. Read the comment docs.

@mnajdova mnajdova changed the title [WIP] feat(docs): adding theme switcher feat(docs): adding theme switcher Sep 26, 2018
# Conflicts:
#	docs/src/routes.tsx
@mnajdova
Copy link
Contributor Author

Merging is now happening in the export of the theme's index file instead of the Provider. This is ready for final review. @levithomason @kuzhelov @miroslavstastny

@layershifter
Copy link
Member

layershifter commented Sep 30, 2018

Are there any real reason to use mobx there? Possible future plans? It looks like shooting a cannon at sparrows 😸

Possible the better solution will be to use Context API?

@mnajdova
Copy link
Contributor Author

mnajdova commented Oct 1, 2018

Are there any real reason to use mobx there? Possible future plans? It looks like shooting a cannon at sparrows 😸

Possible the better solution will be to use Context API?

Yeah, we already discussed that the Context API might be a better option in this case. But now that we have mobx integrated already in this PR, and it might be used in other cases, I don't know if we want to remove all that. But yeah, we are adding that dependency without really having actual need for it. I guess we can switch to the Context API if that's what majority of the people think... @kuzhelov @levithomason thoughts?

@mnajdova
Copy link
Contributor Author

mnajdova commented Oct 1, 2018

Let's hold off merging this until we resolve one more issue. Clicking on a component after having the docs running on the introduction page, changes the font size on all page. See the gif below:

ezgif com-gif-maker 2

It's strange that this is not the same if I directly open some component page in the browser. Will investigate this tomorrow.

@mnajdova
Copy link
Contributor Author

mnajdova commented Oct 1, 2018

Aah found it, something similar with #221 Will confirm latest changes with @kuzhelov and will merge this.

...theme.componentVariables,
[component]: {
...(theme.componentVariables && theme.componentVariables[component]),
[variable]: value,
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuzhelov please take a look into the changes in this file regarding the componentVariables. It seems to work okay in my opinion, but would like to have your approval on this, as the initial changes were yours. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes were made at the moment where there were no mergeThemeVariables yet - now we should rather reuse this functionality here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the state's componentVariable to store just the variables for the component being rendered, and then I am using the mergeThemeVariables for generating the final componentVariables. Please take a look now.


const newTheme: IThemeInput = {
componentVariables: theme.componentVariables,
componentVariables,
Copy link
Contributor

@kuzhelov kuzhelov Oct 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means that theme is only about styles that are provided by CSS-like objects. At the same time, it is possible for theme to provide static CSS styles, and those won't be applied with current implementation. Although I do understand that naively applying them will be a source of the problem as well, still, could you, please, suggest what is our vision for this question?

package.json Outdated
@@ -88,6 +88,8 @@
"fela-plugin-rtl": "^1.0.6",
"keyboard-key": "^1.0.1",
"lodash": "^4.17.10",
"mobx": "^5.1.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another point to rather use React context for achieving the same goal is that currently we have to include mobx to the list of dependencies - while, essentially, this is used only for docs experience (not something that lib consumer is interested in)

Copy link
Contributor

@kuzhelov kuzhelov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just couple of moments that raise my concerns:

  • what are the plans to handle theme's static CSS
  • do we really need Mobx? Absolutely agree with @layershifter's sentiments, as couple of additional points
    • bundle size of Stardust consumed as a library will increase without any good reason for that (as mobx is used in docs only) - it would be much better if we'll consider to introduce default theme instead for much smaller size cost :)
    • even the amount of code necessary will be much lesser if we will use React Context
    • there is no immediate need to use Mobx to solve other problems of the library - and in case if there will be any, it is quite straightforward to integrate Mobx when the problem will shape itself, rather than just trying to predict the problems by advertising tool.

manajdov added 3 commits October 2, 2018 15:15
…ables for the component styling at that moment

-on opening the variables form the merged variables from the theme as well as the state's variables are shown: fixing current issue where the rendered element does not corresponds with the variables values shown in the form
@mnajdova
Copy link
Contributor Author

mnajdova commented Oct 2, 2018

Mobx is now replaced with react context API. Also, fixed bug for not synchronized form values with variables applied in the example. I hope this is ready now for final review.

@@ -23,17 +23,21 @@ import ComponentExampleTitle from './ComponentExampleTitle'
import ContributionPrompt from '../ContributionPrompt'
import getSourceCodeManager, { ISourceCodeManager, SourceCodeType } from './SourceCodeManager'
import { IThemeInput, IThemePrepared } from 'types/theme'
import { mergeThemeVariables } from '../../../../../src/lib/mergeThemes'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -123,6 +129,10 @@ class ComponentExample extends React.PureComponent<IComponentExampleProps, IComp
) {
this.clearActiveState()
}
const { themeName } = nextProps
if (this.state.themeName !== themeName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit unsure - do we really need to store themeName as state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was changed when the name of the theme was fetch from the mobx store, the themeName prop should be enough now. Will change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got it now. This was added because we want to invoke renderSourceCode if the themeName prop is changed. Since we are invoking this method here, we need to have a way to get the new themeName, that's why I have it in the state as well. Any other idea about how can we achive this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, yes - but just to unblock the PR, we could make a notch, merge it and I will try to experiment on that further. To me themeName has clear immutable semantics (that is not example-specific - and this is a key difference from other ones, like showRtl, for example), so this is the reason I'd rather see it being introduced as prop

@@ -346,10 +356,14 @@ class ComponentExample extends React.PureComponent<IComponentExampleProps, IComp
private getDisplayName = () => this.props.examplePath.split('/')[1]

private renderWithProvider(ExampleComponent) {
const { showRtl, theme } = this.state
const { showRtl, componentVariables } = this.state
const { themeName } = this.state
Copy link
Contributor

@kuzhelov kuzhelov Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these two lines could be merged into one. However, concerned whether themeName should be part of state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented on previous comment.

@@ -540,7 +554,10 @@ class ComponentExample extends React.PureComponent<IComponentExampleProps, IComp
</Divider>
<Provider.Consumer
render={({ siteVariables, componentVariables }: IThemePrepared) => {
const variables = componentVariables[displayName]
const mergedVariables = mergeThemeVariables(componentVariables, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we merge them again here? May be missing something, but it seems that we already merged these variables on line 364, so they will be consumed as already merged ones

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me also lot of time to figure this out, but this consumer is for the root Theme Provider, where in contrast, the example, uses it's own provider where these are merged (overriden). This is why we actually have previously bug where the componentVariables were not reflecting the latest status of the variables applied on the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it - still, see it to be a problem that we are merging in two different places of the ComponentExample code. Let me take a look on that after PR will be merged

>
<NewApp />
</Provider>
<NewApp />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to clarify for myself - could you, please, suggest what was the reason of moving (and augmenting) this theme context functionality from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also moved because of the Mobx initial implementation, but I still think the logic for the new Theme context and the Provider should reside in some component file, rather then here. What are your thoughts on this? Maybe the better approach would be creating an App component for all these rather then having them in the routes, but anyway I would like them in some component logic...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus we are using state for the theme context, so we must have some component as a wrapper.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason I see the initial implementation as being more semantically correct is that because routes file will correspond to the app-related aspects, and theming context is something that is not closely related to it. This is a subtle question and I cannot state anything here, but would rather omit these changes from the PR for now. Please, feel free to decide by yourself, though :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced App component that will hold the logic for providing the theme context and the provider.

manajdov added 2 commits October 3, 2018 14:29
-reverted commenting of the merged variables in the variables panel
@mnajdova mnajdova merged commit 5927d6c into master Oct 3, 2018
@levithomason levithomason deleted the feat/theme-switcher branch October 11, 2018 21:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants