Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:

  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

can i get a user's dark mode setting via API?

EvanReese
EvanReese Member, Mentor Posts: 3,110 PRO

It would be cool to make an app always match the Onshape ui without having to control it two places.

Evan Reese
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com

Answers

  • tabetha_bulnes
    tabetha_bulnes Member, Developers, HON-TS Posts: 39 PRO

    You can pull the theming for a user with /users/settings where "theme" 0 = light and 1 = dark. But this only applies to the users Preferences. If they change it for a particular document they are currently in, it might not work.

  • EvanReese
    EvanReese Member, Mentor Posts: 3,110 PRO

    Thanks, Tabetha! I know I've seen other apps that will change if I just change the user setting so maybe that's how its done

    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,968 PRO
    edited October 22

    hey! it's @tabetha_bulnes! 👋😎

    This should work for getting the current browser tab's onshape theme:
    Navigation.Uri.Contains("theme=dark");

    5c664e79-a460-4ccd-827a-453eb7d71dcb_Inbox___Michael_Pascoe___Outlook.gif

    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Derek_Van_Allen_BD
    Derek_Van_Allen_BD Member Posts: 1,171 PRO

    Building on Tabetha's and Michael's answers: Onshape gives the iframe both pieces, so you don't need the API call.

    On load: the iframe URL carries theme=dark or theme=light (Michael's approach). It's whatever theme you're viewing right now, including the quick toggle in the account menu. /users/settings only has the saved preference, which is why it can miss that toggle.

    While open: Onshape posts { messageName: 'onshapeThemeChanged', theme: 'dark' | 'light' } to the iframe whenever the theme changes, whether from the toggle or the saved setting. The catch is that Onshape doesn't post anything to your iframe until your app sends applicationInit first.

    const q = new URLSearchParams(location.search);
    const apply = (theme) => { document.body.dataset.theme = theme; /* redraw canvases etc. */ };
    apply(q.get('theme') === 'light' ? 'light' : 'dark');
    
    const origin = new URL(q.get('server')).origin; // not hard-coded: enterprise domains differ
    window.addEventListener('message', (e) => {
      if (e.source !== window.parent || e.origin !== origin) return;
      if (e.data?.messageName === 'onshapeThemeChanged') apply(e.data.theme);
    });
    window.parent.postMessage({
      messageName: 'applicationInit',
      documentId: q.get('documentId'),
      workspaceId: q.get('workspaceId'),
      elementId: q.get('elementId'),
    }, origin);
    
    

    This is what my Form Studio app does. It works for element tabs and panel extensions; for an extension, put documentId, workspaceId and elementId in your action URL so you have them to send. I haven't seen onshapeThemeChanged in the public docs (Claude found it in Onshape's client code), so it could change.