From 741062bfe2e83f02548fe46e170754feba91ab5f Mon Sep 17 00:00:00 2001 From: Robbie Wagner Date: Fri, 8 Mar 2024 08:40:44 -0500 Subject: [PATCH] Convert to ESM (#2626) * Convert to ESM * More esm tweaks * Update rollup.config.mjs * Tweak deepmerge --- docs-src/tutorials/01-install.md | 2 +- docs-src/tutorials/02-usage.md | 57 +++++----- landing/src/layouts/MainPage.astro | 4 +- package.json | 9 +- pnpm-lock.yaml | 66 ++--------- shepherd.js/package.json | 14 +-- shepherd.js/rollup.config.mjs | 105 +++++------------- shepherd.js/src/step.ts | 4 +- shepherd.js/src/utils/floating-ui.ts | 6 +- test/cypress/.gitignore | 2 - test/cypress/cypress.config.js | 9 +- test/cypress/dummy/index.html | 12 +- .../cypress/examples/destroying-elements.html | 54 +++++---- test/cypress/package.json | 1 + test/cypress/plugins/index.js | 17 --- test/unit/package.json | 1 + 16 files changed, 127 insertions(+), 236 deletions(-) delete mode 100644 test/cypress/plugins/index.js diff --git a/docs-src/tutorials/01-install.md b/docs-src/tutorials/01-install.md index c52c6e1af..e3e661005 100644 --- a/docs-src/tutorials/01-install.md +++ b/docs-src/tutorials/01-install.md @@ -52,7 +52,7 @@ You can use jsDelivr to pull down any release from npm. For example, you could i with: ```html - + ``` diff --git a/docs-src/tutorials/02-usage.md b/docs-src/tutorials/02-usage.md index 87172bca1..2bf0ec6c1 100644 --- a/docs-src/tutorials/02-usage.md +++ b/docs-src/tutorials/02-usage.md @@ -1,8 +1,7 @@ ### Rollup/Webpack Based Builds -The latest versions of Rollup and Webpack support ES6 imports. We have an ES module -exported to `dist/shepherd.esm.js`. This is also specified as `"module"` in -`package.json`, which should allow you to import using standard ES import syntax. +The latest versions of Rollup and Webpack support ES6 imports. Shepherd ships as an ES module +which should allow you to import using standard ES import syntax. i.e. @@ -74,7 +73,7 @@ occurring in _any_ tour: ##### Events -The global Shepherd fires the following events whenever a `Tour` instance fires them. It adds to the object passed to the +The global Shepherd fires the following events whenever a `Tour` instance fires them. It adds to the object passed to the event handlers a `tour` key pointing to the instance which fired the event: - `complete` @@ -88,9 +87,11 @@ event handlers a `tour` key pointing to the instance which fired the event: For multiple events, you can use something like: ```javascript -['complete', 'cancel'].forEach(event => shepherd.on(event, () => { - // some code here -})); +['complete', 'cancel'].forEach((event) => + shepherd.on(event, () => { + // some code here + }) +); ``` ##### Current Tour @@ -125,22 +126,22 @@ const myTour = new Shepherd.Tour(options); - `modalContainer` An optional container element for the modal. If not set, the modal will be appended to `document.body`. - `steps`: An array of step options objects or Step instances to initialize the tour with. - `tourName`: An optional "name" for the tour. This will be appended to the the tour's -dynamically generated `id` property. + dynamically generated `id` property. - `useModalOverlay`: Whether or not steps should be placed above a darkened modal overlay. If true, the overlay will create an opening around the target element so that it can remain interactive. ##### Tour Methods - `addStep(options)`: Creates a new Step object with options, and returns the `Step` instance it created. If the options hash doesn't include an `id`, one will be generated. -You can also pass an existing `Step` instance rather than `options`, but note that Shepherd does not support a Step being attached to multiple Tours. + You can also pass an existing `Step` instance rather than `options`, but note that Shepherd does not support a Step being attached to multiple Tours. - `addSteps([Steps])`: Add multiple steps to the tour - `getById(id)`: Return a step with a specific id - `isActive()`: Check if the tour is active - `next()`: Advance to the next step, in the order they were added - `back()`: Show the previous step, in the order they were added - `cancel()`: Trigger cancel on the current step, hiding it without advancing -- `complete()`: Calls _done() triggering the `complete` event +- `complete()`: Calls \_done() triggering the `complete` event - `hide()`: Hide the current step -- `show([id])`: Show the step specified by id (if it's a string), or index (if it's a number) provided. Defaults to the first step. +- `show([id])`: Show the step specified by id (if it's a string), or index (if it's a number) provided. Defaults to the first step. - `start()`: Show the first step and begin the tour - `getCurrentStep()`: Returns the currently shown step - `removeStep(id)`: Removes the step from the tour @@ -169,7 +170,7 @@ created. - `Function` to be executed when the step is built. It must return one the two options above. - `title`: The step's title. It becomes an `h3` at the top of the step. - `attachTo`: The element the step should be attached to on the page. An object with properties `element` and `on`. - - `element`: An element selector string, a DOM element, or a function (returning a selector, a DOM element, `null` or `undefined`). + - `element`: An element selector string, a DOM element, or a function (returning a selector, a DOM element, `null` or `undefined`). - `on`: The optional direction to place the Floating UI tooltip relative to the element. - Possible string values: 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'right', 'right-start', 'right-end', 'left', 'left-start', 'left-end' @@ -180,15 +181,16 @@ const new Step(tour, { }); ``` -If you don’t specify an `attachTo` the element will appear in the middle of the screen. The same will happen if your +If you don’t specify an `attachTo` the element will appear in the middle of the screen. The same will happen if your `attachTo.element` callback returns `null`, `undefined`, or a selector that does not exist in the DOM. If you omit the `on` portion of `attachTo`, the element will still be highlighted, but the tooltip will appear in the middle of the screen, without an arrow pointing to the target. If the element to highlight does not yet exist while instantiating tour steps, you may use lazy evaluation by supplying a function to `attachTo.element`. The function will be called in the `before-show` phase. + - `beforeShowPromise`: A function that returns a promise. When the promise resolves, the rest of the `show` code for -the step will execute. For example: + the step will execute. For example: ```javascript beforeShowPromise: function() { return new Promise(function(resolve) { @@ -210,16 +212,16 @@ the step will execute. For example: - `secondary`: A boolean, that when true, adds a `shepherd-button-secondary` class to the button - `text`: The HTML text of the button. It can also be a function that returns a string (useful with i18n solutions). - `action`: A function executed when the button is clicked on. - It is automatically bound to the `tour` the step is associated with, so things like `this.next` will - work inside the action. You can use action to skip steps or navigate to specific steps, with something like: - ```javascript - action() { - return this.show('some_step_name'); - } - ``` -- `advanceOn`: An action on the page which should advance shepherd to the next step. It should be an object with a string `selector` and an `event` name. -For example: `{selector: '.some-element', event: 'click'}`. It doesn't have to be an event inside the tour, it can be any event fired on any element on the page. -You can also always manually advance the Tour by calling `myTour.next()`. + It is automatically bound to the `tour` the step is associated with, so things like `this.next` will + work inside the action. You can use action to skip steps or navigate to specific steps, with something like: + ```javascript + action() { + return this.show('some_step_name'); + } + ``` +- `advanceOn`: An action on the page which should advance shepherd to the next step. It should be an object with a string `selector` and an `event` name. + For example: `{selector: '.some-element', event: 'click'}`. It doesn't have to be an event inside the tour, it can be any event fired on any element on the page. + You can also always manually advance the Tour by calling `myTour.next()`. - `highlightClass`: An extra class to apply to the `attachTo` element when it is highlighted (that is, when its step is active). You can then target that selector in your CSS. - `id`: The string to use as the `id` for the step. If an id is not passed one will be generated. - `modalOverlayOpeningPadding`: An amount of padding to add around the modal overlay opening @@ -228,8 +230,9 @@ You can also always manually advance the Tour by calling `myTour.next()`. - `showOn`: A function that, when it returns true, will show the step. If it returns false, the step will be skipped. - `scrollTo`: Should the element be scrolled to when this step is shown? If true, uses the default `scrollIntoView`, if an object, passes that object as the params to `scrollIntoView` i.e. `{behavior: 'smooth', block: 'center'}` - `scrollToHandler`: A function that lets you override the default `scrollTo` behavior and define a custom action to do the scrolling, -and possibly other logic. + and possibly other logic. - `when`: You can define show, hide, etc events inside when. For example: + ```javascript when: { show: function() { @@ -265,14 +268,14 @@ Please note that `complete` and `cancel` are only ever triggered if you call the ### Advancing on Actions -You can use the `advanceOn` option, or the Next button, to advance steps. If you would like however to have a step advance on a +You can use the `advanceOn` option, or the Next button, to advance steps. If you would like however to have a step advance on a complex user action, you can do the following: ```javascript const myStep = myTour.addStep(options); yourApp.on('some-event', () => { - if (myStep.isOpen()){ + if (myStep.isOpen()) { Shepherd.activeTour.next(); } }); diff --git a/landing/src/layouts/MainPage.astro b/landing/src/layouts/MainPage.astro index 486e4de79..ec204a1cb 100644 --- a/landing/src/layouts/MainPage.astro +++ b/landing/src/layouts/MainPage.astro @@ -7,7 +7,7 @@ import { SITE_TITLE } from '../consts'; const { isHome } = Astro.props; const shepherdIncludeCode = ` - + `; --- @@ -177,7 +177,7 @@ const shepherdIncludeCode = ` @@ -121,7 +124,10 @@

Example

- + diff --git a/test/cypress/examples/destroying-elements.html b/test/cypress/examples/destroying-elements.html index bb7dba2b5..ef519d61e 100644 --- a/test/cypress/examples/destroying-elements.html +++ b/test/cypress/examples/destroying-elements.html @@ -1,30 +1,38 @@ - - - - + + + + - - + .second { + right: 0; + background: blue; + } + -
First
-
Second
- +
First
+
Second
+ diff --git a/test/cypress/package.json b/test/cypress/package.json index 168c2bedd..7c4c1d3a2 100644 --- a/test/cypress/package.json +++ b/test/cypress/package.json @@ -7,6 +7,7 @@ "url": "git+https://github.com/shepherd-pro/shepherd.git" }, "license": "MIT", + "type": "module", "scripts": { "cy:open": "./node_modules/.bin/cypress open", "cy:run:chrome": "cypress run --browser chrome", diff --git a/test/cypress/plugins/index.js b/test/cypress/plugins/index.js deleted file mode 100644 index df3a5aeea..000000000 --- a/test/cypress/plugins/index.js +++ /dev/null @@ -1,17 +0,0 @@ -// *********************************************************** -// This example plugins/index.js can be used to load plugins -// -// You can change the location of this file or turn off loading -// the plugins file with the 'pluginsFile' configuration option. -// -// You can read more here: -// https://on.cypress.io/plugins-guide -// *********************************************************** - -// This function is called when a project is opened or re-opened (e.g. due to -// the project's config changing) - -module.exports = (/* on, config */) => { - // `on` is used to hook into various events Cypress emits - // `config` is the resolved Cypress config -}; diff --git a/test/unit/package.json b/test/unit/package.json index 643c156d8..23a817000 100644 --- a/test/unit/package.json +++ b/test/unit/package.json @@ -9,6 +9,7 @@ "license": "MIT", "scripts": { "lint:js": "eslint .", + "rewrite-paths": "replace 'SF:.*src' 'SF:src' coverage/lcov.info", "test:ci": "jest --coverage", "test:watch": "jest --watch", "view-coverage": "http-server -p 9003 ./coverage/lcov-report -o"