Coder Social home page Coder Social logo

crowdstrike / ember-toucan-styles Goto Github PK

View Code? Open in Web Editor NEW
4.0 11.0 3.0 1.16 MB

Ember wrapper, CSS, and JS utilities for working with the Toucan design system

License: MIT License

JavaScript 52.98% TypeScript 34.21% CSS 3.28% HTML 5.13% Handlebars 4.40%

ember-toucan-styles's Introduction

@crowdstrike/ember-toucan-styles

Ember integration for CrowdStrike's design system: Toucan.

Includes:

  • Base CSS and Tailwind configuration automatically integrated into the Ember build pipeline
  • Color variables automatically pulled from Figma
  • Color utilities at @crowdstrike/ember-toucan-styles/utils/colors
  • A base ThemeManager service, for managing the current style theme from JavaScript -- not included by default, but may be extended
  • Testing utilities for qunit tests that affect the current theme.

Setup

Usage

Install

ember install @crowdstrike/ember-toucan-styles @crowdstrike/tailwind-toucan-base

Compatibility

  • ember-source 3.24+
  • ember-auto-import 2+
  • typecsript 4.5+
  • embroider max-compat and max-strict
  • @glimmer/tracking 1.1.2+

Setup

Tailwind 3

App
  1. Create an ember app. You don't have to start with a fresh ember app!

  2. Add tailwind however you like. An easy approach is

# In your terminal
npx ember-apply tailwind
  1. Install this library.
# In your terminal
pnpm add @crowdstrike/ember-toucan-styles @crowdstrike/tailwind-toucan-base
  1. Add the toucan-base plugin to your tailwind config's plugin list.
// config/tailwind/tailwind.config.js
'use strict';

const path = require('path');

const appRoot = path.join(__dirname, '../../');
const appEntry = path.join(appRoot, 'app');
const relevantFilesGlob = '**/*.{html,js,ts,hbs,gjs,gts}';

module.exports = {
  content: [path.join(appEntry, relevantFilesGlob)],
  theme: {
    extend: {},
  },
  presets: [
    require('@crowdstrike/tailwind-toucan-base')
  ],
  safelist: [
    'theme-dark',
    'theme-light',
  ]
};
  1. Create a button to toggle the theme.
# In your terminal
ember g component theme-toggle -gc
  1. Add code to theme-toggle to toggle the theme (and to observe that the theme is toggling). Today, Toucan only supports light and dark mode, so this toggle will flip between the light theme and dark theme.
// app/components/theme-toggle.js
import Component from '@glimmer/component';
import { service } from '@ember/service';

export default class DemoComponent extends Component {
  @service themeManager;

  toggle = () => this.themeManager.toggleTheme();
}
{{! app/components/theme-toggle.hbs }}
<button
  class="
    flex whitespace-nowrap bg-surface-base type-md-tight text-titles-and-attributes
    focus:outline-none p-2 rounded"
  {{on 'click' this.toggle}}
>
  toggle
</button>

More of our colors and tailwind classes can be found here: https://tailwind-toucan-base.pages.dev/

  1. Invoke <ThemeToggle> in app/templates/application.hbs.
<ThemeToggle />
  1. Start both the ember dev server and the tailwind build.
# in terminal 1
pnpm start
# in terminal 2
pnpm tailwind:watch
  1. A local server will boot at http://localhosts:4200 and clicking the rendered button will toggle the background color.

Note that if you're using embroider + webpack, you also have the option to follow the popular guides on setting up tailwind with webpack

Tailwind 2

App

To configure an Ember App, modify:

  • ember-cli-build.js
const EmberApp = require('ember-cli/lib/broccoli/ember-app');

const { configureTailwind } = require('@crowdstrike/ember-toucan-styles/ember-cli');

const tailwindConfig = require('./tailwind.config');

module.exports = function (defaults) {
  let app = new EmberApp(defaults, {
    ...configureTailwind({ tailwindConfig }),
  });

  return app.toTree();
};
  • app/styles/app.css
@tailwind base;
@tailwind components;
@tailwind utilities;

NOTE: if you're also using css-modules, you'll want to import the css-modules output before @tailwind base;

V1 Addon
// ember-cli-build.js

const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

const { configureTailwind } = require('@crowdstrike/ember-toucan-styles/ember-cli');

const tailwindConfig = require('./tailwind.config');

module.exports = function (defaults) {
  let app = new EmberAddon(defaults, {
    ...configureTailwind({ tailwindConfig }),
  });

  return app.toTree();
};
/* tests/dummy/app/styles/app.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

Add ember-cli-postcss to your devDependencies

And lastly, for tests in your addon to have colors, you'll need to set either theme-light or theme-dark on the body class.

CSS Modules in Apps

It is recommended to avoid CSS-Modules, as Tailwind is very flexible -- it may require a different approach to achieve the stylistic goal though.

Follow these steps:

  • remove ember-cli-postcss from your addon

  • install ember-css-modules

  • change ember-cli-build.js

    -const { configureTailwind } = require('@crowdstrike/ember-toucan-styles/ember-cli');
    +const { configureCSSModules } = require('@crowdstrike/ember-toucan-styles/ember-cli');

    To use this in an addon, you'll want to apply these to the options object of the v1 addon's index.js. V2 Addons do not support app-build modifications, so the app would need to configure css-modules support.

Usage

Components may be written following the tailwind documentation. Common CSS classes provided by the Toucan preset for Tailwind can be viewed here ( Source Code here ).

Example:

<button
  class="
    flex whitespace-nowrap bg-transparent type-md-tight text-titles-and-attributes
    focus:outline-none"
  type="button"
>
  A Button!
</button>

Scrollbar Styles

To get toucan-themed scrollbars in browsers that support scrollbar customization

/* some CSS file */
@import '@crowdstrike/ember-toucan-styles/scollbar.css'

or if you have a bundling environment that supports adding CSS as part of the module graph,

// some JavaScript or TypeScript file
import '@crowdstrike/ember-toucan-styles/scrollbar.css';

Using a Custom Theme Manager

Setup

Create an {app,addon}/services/my-theme-manager.js file, and at a minimum,

import { ThemeManager } from '@crowdstrike/ember-toucan-styles';

export default class MyThemeManager extends ThemeManager {
  // your modifications here
}

NOTE:
If you are developing an addon and you want your custom theme-manager to also be called theme-manager, in your package.json, you'll need to specify that your addon runs "after" @crowdstrike/ember-toucan-styles

"ember-addon": {
  "after": [
    "@crowdstrike/ember-toucan-styles"
  ]
}

Setting the Default Theme

Somewhere in the consuming app or addon, run

import { inject as service } from '@ember/service';
import { THEMES } from '@crowdstrike/ember-toucan-styles';

class MyClass {
  @service themeManager;
  // or @service('my-theme-manager') themeManager;

  setup() {
    // using a default theme (THEMES.LIGHT)
    this.themeManager.setup();

    // or with a custom default theme
    this.themeManager.setup(THEMES.DARK);
  }
}

This will first checkout the current-theme key in local storage and if that doesn't exist, the the argument passed to setup() will be used as the default.

Responding to Theme Changes

It is possible to apply certain behaviors when a theme switch occurs, for example:

import { action } from '@ember/object';
import { inject as service } from '@ember/service';

import { THEMES, ThemeManager } from '@crowdstrike/ember-toucan-styles';

import { EVENTS } from '@crowdstrike/ui/analytics/ui';

export default class MyThemeManager extends ThemeManager {
  @service externalGraphics;
  @service analytics;

  @action
  onUpdateTheme(currentTheme, wasSaved = true) {
    let key = trackingKey(currentTheme);

    if (wasSaved) {
      this.analytics.trackEvent(this, key);
    }

    this.externalGraphics.updateTheme(currentTheme);
  }
}

function trackingKey(themeName) {
  switch (themeName) {
    case THEMES.DARK:
      return EVENTS.THEME.DARK;
    case THEMES.LIGHT:
      return EVENTS.THEME.LIGHT;
    default:
      throw new Error(`Theme not recognized: ${themeName}`);
  }
}

Using your own Tailwind Plugins

Add them to your .tailwind.config.js, as normal in https://tailwindcss.com/docs/plugins

ember-toucan-styles's People

Contributors

github-actions[bot] avatar jackrobards avatar mfurniss avatar nicolechung avatar nullvoxpopuli avatar semantic-release-bot avatar ynotdraw avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ember-toucan-styles's Issues

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.