Skip to content

Commit 1577856

Browse files
Dropdown position improvements (#27)
1 parent 42f1af8 commit 1577856

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

components/KernDropdown.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useEffect, useRef, useState } from 'react'
1+
import { Fragment, useEffect, useMemo, useRef, useState } from 'react'
22
import { Menu, Transition } from '@headlessui/react'
33
import { KernDropdownProps } from '../types/dropdown';
44
import { combineClassNames } from '../../javascript-functions/general';
@@ -63,6 +63,16 @@ export default function KernDropdown(props: KernDropdownProps) {
6363
setBackgroundColors(props.backgroundColors.map((x) => "bg-" + reduceColorProperty(x, '100')));
6464
}, [props.backgroundColors]);
6565

66+
const positionDropdown = useMemo(() => {
67+
if (!props.positionDropdown) return "";
68+
switch (props.positionDropdown) {
69+
case "top": return "-translate-x-1/2 left-1/2 bottom-full";
70+
case "bottom": return "-translate-x-1/2 left-1/2 top-full";
71+
case "left": return "-translate-y-1/2 top-1/2 right-full";
72+
case "right": return "-translate-y-1/2 top-1/2 left-full";
73+
}
74+
}, [props.positionDropdown]);
75+
6676
function setOptionsWithCheckboxes(options: any[]) {
6777
if (selectedCheckboxes.length > 0) return;
6878
const newSelectedCheckboxes = options.map((option: any, index: number) => {
@@ -203,7 +213,7 @@ export default function KernDropdown(props: KernDropdownProps) {
203213
leaveTo="transform opacity-0 scale-95"
204214
show={isOpen}
205215
>
206-
<Menu.Items className={`absolute z-10 mt-2 origin-top-right rounded-md bg-white shadow-sm ring-1 ring-black ring-opacity-5 focus:outline-none ${props.dropdownItemsWidth ?? 'w-full'} ${props.dropdownItemsClasses ?? ''}`}>
216+
<Menu.Items className={`absolute z-10 mt-2 origin-top-right rounded-md bg-white shadow-sm ring-1 ring-black ring-opacity-5 focus:outline-none ${props.dropdownItemsWidth ?? 'w-full'} ${props.dropdownItemsClasses ?? ''} ${positionDropdown}`}>
207217
<div className="py-1">
208218
{dropdownCaptions.map((option: any, index: number) => (
209219
<div key={option + "-" + index} className='relative'>

types/dropdown.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @searchTextTyped {function} - returns the text typed in the search bar
3939
* @fontSizeClass {string} - The font size of the dropdown items
4040
* @ignoreDisabledForSearch {boolean} - If the search bar should ignore the disabled options
41+
* @positionDropdown {string} - The position of the dropdown
4142
*/
4243
export type KernDropdownProps = {
4344
buttonName?: string;
@@ -81,6 +82,7 @@ export type KernDropdownProps = {
8182
fontSizeClass?: string;
8283
filteredOptions?: (option: string) => void;
8384
ignoreDisabledForSearch?: boolean;
85+
positionDropdown?: "top" | "bottom" | "left" | "right";
8486
}
8587

8688
export type AppSelectionDropdownProps = {

0 commit comments

Comments
 (0)