Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3052

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T11:08:08+00:00 2024-11-26T11:08:08+00:00

Fluent UI/react-northstar Theming and Component Styling

  • 61k

Fluent UI is a collection of open source user interface components built by Microsoft. It has subsets of libraries for different platforms––React, Windows, iOS––to name a few. To incorporate Fluent UI into a React codebase, it is suggested to use the @fluentui/react package. However, if looking to build a Microsoft Teams app using Fluent UI, the @fluentui/react-northstar package is preferred. At the time of writing, I needed to build an app to upload to Microsoft Teams app marketplace and was exploring v0.62 of @fluentui/react-northstar. Though I was able to find documentation on theming, I wasn’t able to find best practices on consuming the theme and using it in components. Therefore, I created my own solution using React context and I’ll share the gist of my solution in this post.

Set up Theme

In order to use one theme across different components, we need to wrap the components that need theming with Fluent UI’s Provider. Additionally, we can import preset themes to build on top of. The themes available for import include teamsTheme, teamsDarkTheme, and a few other ones, you can refer to their color scheme for colors. I am going to use teamsTheme in my example.

First I created a ThemeProvider to wrap all my components that need to consume the @fluentui/react-northstar library. In my custom theme, I added general theming values under the key siteVariables, and customized component styles under componentVariables and componentStyles, as suggested by the docs.

import React from 'react'; import deepMerge from 'deepmerge'; // a helper to deep merge objects: npmjs.com/package/deepmerge import {   Provider,   teamsTheme, } from '@fluentui/react-northstar';  interface Props {   children: React.ReactNode; }  const customTheme = {   // Adding a few values that teamsTheme does not include,    // for example the spacing variable.   siteVariables: {     spacing: {       unit: 8,     },     colorScheme: {       myCustomBrand: {         background: '#8662b9',         label: '#757b94'       },     },   },   // Customizing the secondary color for all Buttons   // Use this key to customize the behavior when using the   // predefined variables, e.g. <Button secondary />   componentVariables: {     Button: {       secondaryColor: 'orange',     },   },   // Customizing the icon size for all MenuButtons   componentStyles: {     MenuButton: {       icon: {         fontSize: '10px',       },     },   }, };  // Merging my custom theme with the preset teamsTheme const theme = deepMerge(teamsTheme, customTheme); export const ThemeContext = React.createContext(theme);  function ThemeProvider({ children }: Props) {   return (     <Provider theme={theme}>       <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>     </Provider>   ); };  export default ThemeProvider; 
Enter fullscreen mode Exit fullscreen mode

There we go, we now have a theme provider that provides theming to all child components and we've made the theme values accessible via React context.

Accessing Theme & Styling Components

Some solutions for accessing theme and styling component are scattered on the Fluent UI official documentation, including using render props and component variables. Here're a couple examples:

import React from 'react'; import { Provider, Flex, Header, Text, Button } from '@fluentui/react-northstar';  // Example for using Provider.Consumer and render props export function Component() {   return (     <Provider.Consumer       render={theme => {         return (           <Flex wrap gap="gap.smaller">             <Header as="h2" content="Happy Summer"/>             <Text content="It's watermelon time!"/>           </Flex>         );       }}     />   ); }  // Example for using component level styling export function AnotherComponent() {     return (     <div>         <Text>Get cool!</Text>         <Button             content="Unlock the summer"             variables={{               color: 'watermelon',               backgroundColor: 'green',               paddingLeftRightValue: 30,             }}         />     </div>     ) } 
Enter fullscreen mode Exit fullscreen mode

I find the render prop API not as composable, and mixing it with the hook interface and passing theme into styles are messy. As for using the component level variables, it doesn’t automatically give us access to the theme, unless we wrap that component inside the theme consumer render props, which is again, not as neat.

Therefore, I created theme context in the ThemeProvider above. Then in my component, I can use React.useContext to access the theme.

For styling my components, I’m using the useStyles pattern to apply styles using CSS-in-JS inside each component and allow passing theme as argument. The advantage of this solution is that accessing theme and passing it for styling is easy, and we can extend the useStyles pattern to accept other props and have business logic impact component styling if needed.

// Example component import React, { useContext } from 'react'; import { Provider, Flex, Header, Text } from '@fluentui/react-northstar'; import { ThemeContext } from './ThemeProvider'; // This is the ThemeProvider I created up top  function Component() {   const themeContext = useContext(ThemeContext);   const styles = useStyles(themeContext);    return (     <Flex wrap gap="gap.smaller" style={styles.root}>       <Header as="h2" content="Happy Summer" style={styles.header}/>       <Text content="It's watermelon time!" style={styles.description}/>     </Flex>   ); }  const useStyles = (theme) => ({   root: {       padding: theme.siteVariables.spacing.unit * 2   },   header: {       backgroundColor: theme.siteVariables.colorScheme.myCustomBrand.background   },   description: {       marginTop: theme.siteVariables.spacing.unit   }, });  export default Component; 
Enter fullscreen mode Exit fullscreen mode

That’s it! Let me know what you think 🙂

javascriptmicrosoftteamsappsreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.