jQuery Smart Wizard

repository·master·Indexed 21 days ago

https://github.com/techlab/jquery-smartwizard

A lightweight, accessible, and extensible step wizard plugin for jQuery (v7.0.2) designed for multi-step workflows like registration forms and checkouts. Features include customizable themes, transitions, TypeScript support, and a redesigned API in v7. It supports jQuery >= 1.11.1 and provides a comprehensive set of public methods and lifecycle events (e.g., leave.sw, shown.sw) to manage step navigation and state.

Tokens
10.1K
Snippets
28
Records
35
Agent score
67%

What's inside jquery-smartwizard

  1. New Features in SmartWizard v7

    master

    SmartWizard v7 introduces several new capabilities:

    • displayMode: Controls the visual theme mode. Options: 'auto' (follows system preference), 'dark', 'light', or 'none'.
    • swipeNavigation: Enables swipe left/right gestures on touch devices to navigate between steps.
    • Horizontal nav scroll: Automatically enables horizontal scrolling of the navigation bar via mouse wheel when steps overflow.
    • TypeScript support: Full type definitions are provided in dist/types/.
  2. Integrate jQuery Smart Wizard with Bundlers (ESM/Webpack)

    master

    For modern build pipelines using ES Modules or Webpack, import jQuery, the required CSS files, and the smartWizard module.

    ES Module / Bundler Example:

    import $ from "jquery";
    import "smartwizard/dist/css/smartwizard.min.css";
    import "smartwizard/dist/css/themes/arrows.min.css";
    import smartWizard from "smartwizard";
    
    $(function() {
        $('#smartwizard').smartWizard();
    });

    CommonJS / Webpack Example:

    var $ = require("jquery");
    require("smartwizard/dist/css/smartwizard.min.css");
    require("smartwizard/dist/css/themes/arrows.min.css");
    const smartWizard = require("smartwizard");
    
    $(function() {
        $('#smartwizard').smartWizard();
    });
    import $ from "jquery";
    import "smartwizard/dist/css/smartwizard.min.css";
    import "smartwizard/dist/css/themes/arrows.min.css";
    import smartWizard from "smartwizard";
    
    $(function() {
        $('#smartwizard').smartWizard();
    });
  3. Install jQuery Smart Wizard v7

    master

    You can install jQuery Smart Wizard using package managers, CDNs, or by downloading the source.

    Requirements:

    • jQuery >= 1.11.1 (Supports 3.x, 4.x, and Slim versions).

    NPM:

    npm install smartwizard

    Yarn:

    yarn add smartwizard

    CDN (jsDelivr): Include the base CSS, a theme CSS, and the JavaScript file in your HTML.

    <!-- CDN - jsDelivr Example -->
    <link href="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/smartwizard.min.css" rel="stylesheet" type="text/css" />
    <link href="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/themes/arrows.min.css" rel="stylesheet" type="text/css" />
    <script src="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/js/jquery.smartWizard.min.js" type="text/javascript"></script>
  4. Install SmartWizard v7 via CDN

    master

    In v7, the single smart_wizard_all.min.css file has been split. You must include the base CSS file and your chosen theme CSS file separately.

    Base CSS: https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/smartwizard.min.css

    Theme CSS (example: arrows): https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/themes/arrows.min.css

    JavaScript: https://cdn.jsdelivr.net/npm/smartwizard@7/dist/js/jquery.smartWizard.min.js

    <!-- Base CSS -->
    <link href="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/smartwizard.min.css" rel="stylesheet" />
    
    <!-- Theme CSS (choose one) -->
    <link href="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/css/themes/arrows.min.css" rel="stylesheet" />
    
    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/smartwizard@7/dist/js/jquery.smartWizard.min.js"></script>
  5. Implement the SmartWizard HTML Structure

    master

    To use SmartWizard, you must follow a specific HTML structure consisting of a navigation list (ul.nav) and a content container (div.tab-content).

    1. Navigation: A ul with class nav containing li.nav-item elements. Each item should have an a.nav-link with an href pointing to the step ID.
    2. Content: A div.tab-content containing div.tab-pane elements with IDs matching the navigation links.
    3. Progress Bar (Optional): A div.progress containing a div.progress-bar.
    <!-- SmartWizard HTML Structure -->
    <div id="smartwizard">
        <ul class="nav">
            <li class="nav-item">
              <a class="nav-link" href="#step-1">
                <div class="badge">1</div>
                Step Title
              </a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#step-2">
                <span class="badge">2</span>
                Step Title
              </a>
            </li>
        </ul>
    
        <div class="tab-content">
            <div id="step-1" class="tab-pane" role="tabpanel" aria-labelledby="step-1">
                Step 1 content
            </div>
            <div id="step-2" class="tab-pane" role="tabpanel" aria-labelledby="step-2">
                Step 2 content
            </div>
        </div>
    
        <!-- Optional progressbar -->
        <div class="progress">
          <div class="progress-bar" role="progressbar" style="width: 0%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
        </div>
    </div>
  6. Migrate from SmartWizard v6 to v7

    master

    v7 is a complete TypeScript rewrite of the plugin. While the HTML structure remains unchanged, the core API surface has been redesigned. Key changes include restructured option names, new event namespaces, and updated public method signatures.

    Migration Checklist:

    1. Update package to smartwizard@latest.
    2. Update CSS imports (split into base and theme files).
    3. Rename options according to the new logical grouping.
    4. Update event listeners to use the .sw suffix and the new args object pattern.
    5. Update setState/unsetState from 'disabled' to 'disable'.
    6. Replace fixHeight with adjustHeight.
    7. Update CSS transition configurations.
    npm install smartwizard@latest
  7. Configure SmartWizard Options

    master

    Initialize the wizard with an options object to control behavior, themes, transitions, and navigation.

    Key Configuration Groups:

    • initialStep: Index of the first step (default 0).
    • theme: Visual style (basic | arrows | dots | round | square | progress).
    • displayMode: auto | dark | light | none.
    • behavior: Includes autoHeight and useUrlHash.
    • navigation: Controls anchor clicks and completed step behavior.
    • transition: Defines animation effect (default | fade | slideHorizontal | slideVertical | slideSwing | css), speed, and easing.
    • toolbar: Position and button visibility (showNext, showPrevious).
    • stepStates: Pre-set states for steps (completed, disabled, hidden, error, warning).
    • contentLoader: A callback function for dynamic content loading.
    $('#smartwizard').smartWizard({
        initialStep: 0,
        theme: 'basic',
        displayMode: 'auto',
        behavior: {
            autoHeight: true,
            useUrlHash: false,
            supportBrowserHistory: false
        },
        navigation: {
            enabled: true,
            alwaysClickable: false,
            completed: {
                enabled: true,
                completeAllPreviousSteps: true,
                clearOnBack: false,
                clickable: true
            }
        },
        transition: {
            effect: 'default',
            speed: 400,
            easing: '',
            css: {
                prefix: '',
                forward:  { show: '', hide: '' },
                backward: { show: '', hide: '' }
            }
        },
        toolbar: {
            position: 'bottom',
            buttons: {
                showNext: true,
                showPrevious: true
            },
            extraElements: ''
        },
        keyboardNavigation: {
            enabled: true,
            keys: {
                left: [37],
                right: [39]
            }
        },
        swipeNavigation: {
            enabled: false,
            threshold: 50
        },
        localization: {
            buttons: {
                next: 'Next',
                previous: 'Previous'
            }
        },
        stepStates: {
            completed: [],
            disabled: [],
            hidden: [],
            error: [],
            warning: []
        },
        scrollToView: false,
        contentLoader: null
    });
  8. Understand StepState values

    master

    Steps in the wizard can exist in several states, which can be used to drive CSS styling or logic:

    • 'default': Initial state.
    • 'active': The currently viewed step.
    • 'completed': A step that has been successfully visited.
    • 'disabled': A step that cannot be visited.
    • 'hidden': A step that is conditionally not shown.
    • 'error': A step where validation failed.
    • 'warning': A step with a soft validation warning.
  9. Configure SmartWizard v7 Options

    master

    Options in v7 are restructured into logical groups. Below is the mapping for common configuration changes from v6 to v7.

    v6 Optionv7 Option
    selectedinitialStep
    autoAdjustHeightbehavior.autoHeight
    backButtonSupportbehavior.supportBrowserHistory
    enableUrlHashbehavior.useUrlHash
    transition.animationtransition.effect
    transition.prefixCsstransition.css.prefix
    toolbar.showNextButtontoolbar.buttons.showNext
    anchor.enableNavigationnavigation.enabled
    lang.nextlocalization.buttons.next
    disabledStepsstepStates.disabled
    getContentcontentLoader
    $('#smartwizard').smartWizard({
        initialStep: 0,
        theme: 'arrows',
        displayMode: 'auto', // NEW: 'auto' | 'dark' | 'light' | 'none'
        behavior: {
            autoHeight: true,
            supportBrowserHistory: true,
            useUrlHash: false,
        },
        transition: {
            effect: 'css',
            speed: 400,
            easing: '',
            css: {
                prefix: 'animate__animated',
                forward:  { show: 'animate__fadeIn',  hide: 'animate__fadeOut' },
                backward: { show: 'animate__fadeIn',  hide: 'animate__fadeOut' },
            },
        },
        toolbar: {
            position: 'bottom',
            buttons: {
                showNext: true,
                showPrevious: true,
            },
            extraElements: '<button>Custom</button>',
        },
        navigation: {
            enabled: true,
            alwaysClickable: false,
            completed: {
                enabled: true,
                completeAllPreviousSteps: true,
                clearOnBack: false,
                clickable: true,
            },
        },
        keyboardNavigation: {
            enabled: true,
            keys: {
                left: [37],
                right: [39],
            },
        },
        swipeNavigation: {  // NEW
            enabled: false,
            threshold: 50,
        },
        localization: {
            buttons: {
                next: 'Next',
                previous: 'Previous',
            },
        },
        stepStates: {
            disabled: [2],
            error: [],
            warning: [],
            hidden: [],
            completed: [],
        },
        contentLoader: null,
    });
  10. Use SmartWizard Public Methods

    master

    Control the wizard instance using the .smartWizard() jQuery method with specific command strings.

    Navigation Methods:

    • next: Navigate to the next step.
    • prev: Navigate to the previous step.
    • goToStep, index: Go to a specific step index.
    • goToStep, index, true: Force navigation to a step and mark previous steps as done.
    • reset: Reset the wizard to its initial state.

    State and Configuration Methods:

    • setOptions, options: Update configuration at runtime.
    • setState, [indexes], state: Set state for steps (e.g., 'disable', 'error').
    • unsetState, [indexes], state: Remove a state from steps.
    • loader, 'show' | 'hide': Toggle the loading indicator.
    • adjustHeight: Manually trigger content height adjustment.
    • getStepInfo: Returns { currentStep, totalSteps }.
    const wizard = $('#smartwizard');
    
    wizard.smartWizard('next');
    wizard.smartWizard('prev');
    wizard.smartWizard('goToStep', 2);
    wizard.smartWizard('goToStep', 2, true);
    wizard.smartWizard('reset');
    wizard.smartWizard('setOptions', { theme: 'arrows' });
    
    wizard.smartWizard('setState', [1, 2], 'disable');
    wizard.smartWizard('setState', [3], 'error');
    wizard.smartWizard('unsetState', [1, 2], 'disable');
    
    wizard.smartWizard('loader', 'show');
    wizard.smartWizard('loader', 'hide');
    
    wizard.smartWizard('adjustHeight');
    
    const info = wizard.smartWizard('getStepInfo');
  11. Handle SmartWizard Events

    master

    Listen to lifecycle events to implement custom logic, such as form validation or post-step actions.

    Available Events:

    • initialized.sw: Fires after the wizard is fully initialized.
    • loaded.sw: Fires after the initial step is shown for the first time.
    • leave.sw: Fires before leaving the current step. Note: Returning false from the handler will cancel navigation.
      • Arguments: { stepIndex, nextStepIndex, stepElement, stepDirection, stepPosition }.
    • shown.sw: Fires after a new step is shown.
      • Arguments: { stepIndex, stepElement, stepDirection, stepPosition }.
    // Example: Prevent navigation if validation fails
    $('#smartwizard').on('leave.sw', function(e, args) {
        const isValid = validateCurrentStep();
        if (!isValid) {
            return false; // Cancel navigation
        }
    });
    
    // Example: React to step being shown
    $('#smartwizard').on('shown.sw', function(e, args) {
        console.log('Step shown:', args.stepIndex);
    });
  12. Use SmartWizard v7 Public Methods

    master

    Most public methods remain unchanged, but some have been renamed or updated.

    MethodStatusNotes
    smartWizard('goToStep', idx)Unchanged
    smartWizard('next')Unchanged
    smartWizard('prev')Unchanged
    smartWizard('reset')Unchanged
    smartWizard('adjustHeight')RenamedFormerly fixHeight
    smartWizard('setState', [n], 'disable')UpdatedState name changed from 'disabled' to 'disable'
    smartWizard('unsetState', [n], 'disable')UpdatedState name changed from 'disabled' to 'disable'
    smartWizard('setOptions', opts)UnchangedNow also calls load()
    smartWizard('loader', 'show')Unchanged
    smartWizard('getStepInfo')Unchanged