🎥 Make videos programmatically with React
Elements: Add search to embedded Element library
Igor Samokhovets1414fb3cafc5
diff --git a/packages/docs/src/components/Elements/ElementLibrary.module.css b/packages/docs/src/components/Elements/ElementLibrary.module.cssindex c462763f7a..310ae30124 100644--- a/packages/docs/src/components/Elements/ElementLibrary.module.css+++ b/packages/docs/src/components/Elements/ElementLibrary.module.css@@ -3,6 +3,110 @@margin-top: 32px;}+.searchToolbar {+ display: flex;+ flex-wrap: wrap;+ align-items: center;+ justify-content: space-between;+ gap: 12px 20px;+ margin-bottom: 28px;+}++.searchField {+ display: flex;+ min-width: 0;+ max-width: 440px;+ flex: 1 1 240px;+ align-items: center;+ gap: 10px;+ border: 1px solid var(--ifm-color-emphasis-300);+ border-radius: 8px;+ padding: 0 10px 0 12px;+ background: var(--ifm-background-surface-color);+ color: var(--ifm-color-emphasis-600);+}++.searchField:focus-within {+ border-color: var(--ifm-color-primary);+ outline: 2px solid var(--ifm-color-primary);+ outline-offset: 2px;+}++.searchIcon {+ width: 18px;+ height: 18px;+ flex-shrink: 0;+}++.searchInput {+ width: 100%;+ min-width: 0;+ height: 42px;+ border: 0;+ padding: 0;+ outline: none;+ background: transparent;+ color: var(--ifm-font-color-base);+ font: inherit;+ font-size: 0.9375rem;+}++.searchInput::placeholder {+ color: var(--ifm-color-emphasis-600);+}++.searchInput::-webkit-search-cancel-button,+.searchInput::-webkit-search-decoration {+ appearance: none;+}++.clearSearch {+ display: flex;+ width: 28px;+ height: 28px;+ flex-shrink: 0;+ align-items: center;+ justify-content: center;+ border: 0;+ border-radius: 4px;+ padding: 5px;+ background: transparent;+ color: var(--ifm-color-emphasis-600);+ cursor: default;+}++.clearSearch:hover {+ color: var(--ifm-font-color-base);+}++.clearSearch:focus-visible {+ outline: 2px solid var(--ifm-color-primary);+ outline-offset: 2px;+}++.clearSearch svg {+ width: 100%;+ height: 100%;+}++.resultCount {+ color: var(--ifm-color-emphasis-600);+ font-size: 0.8125rem;+}++.emptyState {+ border: 1px dashed var(--ifm-color-emphasis-300);+ border-radius: 8px;+ padding: 40px 20px;+ text-align: center;+}++.emptyState p {+ margin: 8px 0 0;+ color: var(--ifm-color-emphasis-600);+ font-size: 0.875rem;+}+.section + .section {margin-top: 48px;}diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsxindex ee9f07ace1..f2c0e0e67a 100644--- a/packages/docs/src/components/Elements/ElementLibrary.tsx+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx@@ -13,6 +13,7 @@ import {setElementDragImage,} from './element-drag-data';import {+ getElementCategoryLabel,getElementDocumentationUrl,getElementLibrarySections,type ElementCategory,@@ -277,14 +278,41 @@ export const ElementLibrary: React.FC<{readonly category: ElementCategory | null;readonly sourceCodeBySlug: Readonly<Record<string, string>>;}> = ({category, sourceCodeBySlug}) => {- const sections = getElementLibrarySections(category);const prefersReducedMotion = usePrefersReducedMotion();+ const [isStudioEmbed, setIsStudioEmbed] = useState(false);const [isCaptionPicker, setIsCaptionPicker] = useState(false);const [selectedSlug, setSelectedSlug] = useState<string | null>(null);+ const [query, setQuery] = useState('');+ const searchRef = useRef<HTMLInputElement>(null);+ const searchTerms = isStudioEmbed+ ? query.trim().toLowerCase().split(/\s+/).filter(Boolean)+ : [];+ const sections = getElementLibrarySections(category)+ .map((section) => ({+ ...section,+ definitions: section.definitions.filter((definition) => {+ const searchText = [+ definition.displayName,+ definition.description,+ definition.slug.replace(/[-/]/g, ' '),+ section.label,+ ]+ .join(' ')+ .toLowerCase();+ return searchTerms.every((term) => searchText.includes(term));+ }),+ }))+ .filter((section) => section.definitions.length > 0);+ const resultCount = sections.reduce(+ (count, section) => count + section.definitions.length,+ 0,+ );useLayoutEffect(() => {+ const isEmbedded = isInsideStudio();+ setIsStudioEmbed(isEmbedded);setIsCaptionPicker(- isInsideStudio() &&+ isEmbedded &&new URLSearchParams(window.location.search).get('remotion-studio-context',) === 'captions',@@ -295,6 +323,76 @@ export const ElementLibrary: React.FC<{return (<div className={styles.library}>+ {isStudioEmbed ? (+ <div className={styles.searchToolbar}>+ <div+ aria-label="Elements"+ className={styles.searchField}+ role="search"+ >+ <svg+ aria-hidden="true"+ className={styles.searchIcon}+ fill="none"+ viewBox="0 0 24 24"+ stroke="currentColor"+ strokeWidth="1.75"+ >+ <circle cx="10.5" cy="10.5" r="6.5" />+ <path d="m16 16 4.5 4.5" />+ </svg>+ <input+ ref={searchRef}+ aria-label="Search elements"+ className={styles.searchInput}+ onChange={(event) => setQuery(event.currentTarget.value)}+ onKeyDown={(event) => {+ if (event.key === 'Escape' && query !== '') {+ event.preventDefault();+ event.stopPropagation();+ setQuery('');+ }+ }}+ placeholder="Search elements…"+ type="search"+ value={query}+ />+ {query !== '' ? (+ <button+ aria-label="Clear search"+ className={styles.clearSearch}+ onClick={() => {+ setQuery('');+ searchRef.current?.focus();+ }}+ type="button"+ >+ <svg+ aria-hidden="true"+ fill="none"+ viewBox="0 0 24 24"+ stroke="currentColor"+ strokeWidth="1.75"+ >+ <path d="m6 6 12 12M6 18 18 6" />+ </svg>+ </button>+ ) : null}+ </div>+ <div aria-atomic="true" className={styles.resultCount} role="status">+ {resultCount} {resultCount === 1 ? 'element' : 'elements'}+ {category === null+ ? null+ : ` in ${getElementCategoryLabel(category)}`}+ </div>+ </div>+ ) : null}+ {sections.length === 0 ? (+ <div className={styles.emptyState}>+ <strong>No elements found</strong>+ <p>Try another search or browse another category.</p>+ </div>+ ) : null}{sections.map((section) => {if (category !== null) {return (