@devdocsai/web
A prebuilt version of the DevDocs.ai dialog, based on @devdocsai/react
, built
with Preact for bundle-size savings. Viable for use from vanilla JavaScript or
any framework.
Installation
Install the package from NPM:
npm add @devdocsai/web @devdocsai/css
Usage
Include the CSS on your page, via a link tag or by importing it in your
JavaScript:
<link rel="stylesheet" href="https://esm.sh/@devdocsai/css@0.5.1?css" />
import '@devdocsai/css';
Call the devdocsai
function with your project key:
import { devdocsai } from '@devdocsai/web';
const devdocsaiEl = document.querySelector('#devdocsai');
devdocsai('YOUR-PROJECT-KEY', devdocsaiEl, {
references: {
getHref: (reference) => reference.file.path.replace(/\.[^.]+$/, '');
getLabel: (reference) => {
return reference.meta?.leadHeading?.value || reference.file?.title;
}
},
});
where YOUR-PROJECT-KEY
can be obtained in your project settings on
devdocs.ai.
Options are optional and allow you to configure the texts and links used in the
component to some extent. You will most likely want to pass references.getHref
and reference.getLabel
to transform your prompt references into links to your
corresponding documentation, and search.getHref
to transform search result
paths into links to your documentation.
import type {
SubmitFeedbackOptions,
SubmitPromptOptions,
SubmitSearchQueryOptions,
} from '@devdocsai/core';
interface DevDocsAIOptions {
display?: 'plain' | 'dialog';
defaultView?: 'search' | 'chat' | 'prompt';
close?: {
label?: string;
visible?: boolean;
};
description?: {
hide?: boolean;
text?: string;
};
feedback?: SubmitFeedbackOptions & {
enabled?: boolean;
heading?: string;
onFeedbackSubmit?: (
feedback: PromptFeedback,
messages: ChatViewMessage[],
promptId?: string,
) => void;
};
chat?: SubmitChatOptions & {
enabled?: boolean;
label?: string;
tabLabel?: string;
placeholder?: string;
showSender?: boolean;
history?: boolean;
defaultView?: {
message?: string | ReactElement;
promptsHeading?: string;
prompts?: string[];
};
};
prompt?: SubmitChatOptions & {
label?: string;
tabLabel?: string;
placeholder?: string;
defaultView?: {
message?: string | ReactElement;
promptsHeading?: string;
prompts?: string[];
};
};
references?: {
display?: 'none' | 'end';
getHref?: (reference: FileSectionReference) => string | undefined;
getLabel?: (reference: FileSectionReference) => string | undefined;
heading?: string;
loadingText?: string;
transformReferenceId?: (referenceId: string) => {
href: string;
text: string;
};
};
search?: SubmitSearchQueryOptions & {
enabled?: boolean;
getHref?: (
reference: SearchResult | AlgoliaDocSearchHit,
) => string | undefined;
getHeading?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
getTitle?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
getSubtitle?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
label?: string;
tabLabel?: string;
placeholder?: string;
};
trigger?: {
label?: string;
buttonLabel?: string;
placeholder?: string;
floating?: boolean;
customElement?: boolean;
iconSrc?: string;
};
title?: {
hide?: boolean;
text?: string;
};
showBranding?: boolean;
debug?: boolean;
}
Styles are easily overridable for customization via targeting classes.
Additionally, see the styling section in
our documentation for a full list of variables.
Usage via <script>
tag
Besides initializing the DevDocs.ai component yourselves from JavaScript, you
can load the script from a CDN. You can attach the options for the DevDocs.ai
component to the window prior to loading our script:
<link rel="stylesheet" href="https://esm.sh/@devdocsai/css@0.13.x?css" />
<script>
window.devdocsai = {
projectKey: `YOUR-PROJECT-KEY`,
container: `#devdocsai`,
options: {
references: {
getHref: (reference) => reference.file?.path?.replace(/\.[^.]+$/, ''),
getLabel: (reference) => {
return reference.meta?.leadHeading?.value || reference.file?.title;
},
},
},
};
</script>
<script type="module" src="https://esm.sh/@devdocsai/web@0.16.x/init"></script>
<div id="devdocsai"></div>
API
devdocsai(projectKey, container, options?)
Render a DevDocs.ai dialog button.
Arguments
projectKey
(string
): Your DevDocs.ai project key.container
(HTMLElement | string
): The element or selector to render
DevDocs.ai into.options
(object
): Options for customizing DevDocs.ai, see above.
When rendering the DevDocs.ai component, it will render a search input-like
button by default. You have two other options:
-
set trigger.floating = true
to render a floating button
-
set trigger.customElement = true
, then
import { openDevDocsAI } from '@devdocsai/react'
and call
openDevDocsAI()
from your code. This gives you the flexibility to render
your own trigger element and attach whatever event handlers you would like
and/or open the DevDocs.ai dialog programmatically.
devdocsaiOpen()
Open the DevDocs.ai dialog programmatically.
devdocsaiClose()
Close the DevDocs.ai dialog programmatically.
devdocsaiChat(projectKey, container, options?)
Render the DevDocs.ai chat view standalone, outside of a dialog.
projectKey
(string
): Your DevDocs.ai project key.container
(HTMLElement | string
): The element or selector to render
DevDocs.ai into.options
(object
): Options for customizing DevDocs.ai, see above.options.chatOptions
(DevDocsAIOptions.chat
): Enable and configure chat
functionality. Allows users to have a conversation with an assistant.
Enabling chat functionality will disable prompt functionality. See above for options.options.debug
(boolean
): Display debug infooptions.feedbackOptions
(DevDocsAIOptions.feedback
): Enable feedback
functionality, shows a thumbs up/down button after a prompt was submitted. See above for options.options.referencesOptions
(DevDocsAIOptions.references
): Enable and
configure references functionality. See above for options.
Documentation
The full documentation for @devdocsai/web
can be found on the
DevDocs.ai docs.
Authors
This library is created by the team behind DevDocs.ai
(DevDocs.work). DevDocs.work also provides technical writing, software development, custom AI, design, and other consulting services.
License
MIT © DevDocs.ai