Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko committed Jun 11, 2024
1 parent b223779 commit 9b2375f
Show file tree
Hide file tree
Showing 7 changed files with 1,090 additions and 27 deletions.
19 changes: 19 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ Example:
{% endlist %}
```

Additionally, you can use radiobatons using a contruction

```
{% list tabs vertical %}
- Название таба 1
Текст таба 1.
* Можно использовать списки.
* И **другую** разметку.
- Название таба 2
Текст таба 2.
{% endlist %}
```

The keys for the tabs are generated automatically. They are based on the tab's names using the github anchors style.

You can set your own keys for tabs with this statement:
Expand Down
2 changes: 2 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {TabsOrientation} from './plugin/transform';
import type {TabsController} from './runtime/TabsController';

export const TABS_CLASSNAME = 'yfm-tabs';
Expand All @@ -20,6 +21,7 @@ export const DEFAULT_TABS_GROUP_PREFIX = 'defaultTabsGroup-';
export interface Tab {
group?: string;
key: string;
align: TabsOrientation;
}

export interface SelectedTabEvent {
Expand Down
2 changes: 1 addition & 1 deletion src/react/useDiplodocTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useDiplodocTabs(callback: UseDiplodocTabsCallback) {
),
// @todo remove
selectTab: useCallback(

Check failure on line 26 in src/react/useDiplodocTabs.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `⏎············(tab:·Tab)·=>·window[GLOBAL_SYMBOL].selectTab(tab),⏎············[],⏎········` with `(tab:·Tab)·=>·window[GLOBAL_SYMBOL].selectTab(tab),·[]`
(tab: Tab) => window[GLOBAL_SYMBOL].selectTab(tab, 'horizontal'),
(tab: Tab) => window[GLOBAL_SYMBOL].selectTab(tab),
[],
),
};
Expand Down
39 changes: 16 additions & 23 deletions src/runtime/TabsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ export class TabsController {
}

const tab = this.getTabDataFromHTMLElement(target);
const align: TabsOrientation = areVertical ? 'vertical' : 'horizontal';

if (tab) {
this._selectTab(tab, align, target);
this._selectTab(tab, target);
}
});
this._document.addEventListener('keydown', (event) => {
Expand Down Expand Up @@ -94,12 +93,11 @@ export class TabsController {
return;
}

const align = this.getTabsOrientation(target);

Check failure on line 96 in src/runtime/TabsController.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `⏎`
const newIndex =
(currentTabIndex + (direction === 'left' ? -1 : 1) + tabs.length) % tabs.length;

this.selectTab(tabs[newIndex], align);
this.selectTab(tabs[newIndex]);
nodes[newIndex].focus();
});
}
Expand All @@ -121,24 +119,23 @@ export class TabsController {
return;
}

const align = this.getTabsOrientation(target);

const tab = this.getTabDataFromHTMLElement(target);

if (tab) {
this._selectTab(tab, align, target);
this._selectTab(tab, target);
}

if (options?.scrollToElement) {
target.scrollIntoView();
}
}

selectTab(tab: Tab, align: TabsOrientation) {
this._selectTab(tab, align);
selectTab(tab: Tab) {
this._selectTab(tab);
}

private _selectTab(tab: Tab, align: TabsOrientation, targetTab?: HTMLElement) {
const {group, key} = tab;
private _selectTab(tab: Tab, targetTab?: HTMLElement) {
const {group, key, align} = tab;

if (!group) {
return;
Expand All @@ -148,10 +145,10 @@ export class TabsController {
const previousTargetOffset =
scrollableParent && getOffsetByScrollableParent(targetTab, scrollableParent);

const updatedTabs = this.updateHTML({group, key}, align);
const updatedTabs = this.updateHTML({group, key, align}, align);

if (updatedTabs > 0) {
this.fireSelectTabEvent({group, key}, targetTab?.dataset.diplodocId);
this.fireSelectTabEvent({group, key, align}, targetTab?.dataset.diplodocId);

if (previousTargetOffset) {
this.resetScroll(targetTab, scrollableParent, previousTargetOffset);
Expand Down Expand Up @@ -266,9 +263,9 @@ export class TabsController {
}

private fireSelectTabEvent(tab: Required<Tab>, diplodocId?: string) {
const {group, key} = tab;
const {group, key, align} = tab;

const eventTab: Tab = group.startsWith(DEFAULT_TABS_GROUP_PREFIX) ? {key} : tab;
const eventTab: Tab = group.startsWith(DEFAULT_TABS_GROUP_PREFIX) ? {key, align} : tab;

this._onSelectTabHandlers.forEach((handler) => {
handler({tab: eventTab, currentTabId: diplodocId});
Expand All @@ -290,24 +287,18 @@ export class TabsController {
return target.dataset.diplodocVerticalTab || Boolean(parent?.dataset.diplodocVerticalTab);
}

private getTabsOrientation(target: HTMLElement): TabsOrientation {
const areVertical = this.areTabsVertical(target);

return areVertical ? 'vertical' : 'horizontal';
}

private getTabDataFromHTMLElement(target: HTMLElement): Tab | null {
if (this.areTabsVertical(target)) {
const tab = target.dataset.diplodocVerticalTab ? target : target.parentElement!;

Check warning on line 292 in src/runtime/TabsController.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Forbidden non-null assertion

const key = tab.dataset.diplodocKey;
const group = (tab.closest(Selector.TABS) as HTMLElement)?.dataset.diplodocGroup;
return key && group ? {group, key} : null;
return key && group ? {group, key, align: 'vertical'} : null;
}

const key = target.dataset.diplodocKey;
const group = (target.closest(Selector.TABS) as HTMLElement)?.dataset.diplodocGroup;
return key && group ? {group, key} : null;
return key && group ? {group, key, align: 'horizontal'} : null;
}

private getTabs(target: HTMLElement): {tabs: Tab[]; nodes: NodeListOf<HTMLElement>} {
Expand All @@ -323,9 +314,11 @@ export class TabsController {
return;
}

/** horizontal-only supported feature (used in left/right button click) */
tabs.push({
group,
key,
align: 'horizontal',
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/scss/tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
line-height: 18px;
user-select: none;
}

.yfm-vertical-tab > label:before {
content: "";
position: absolute;
Expand All @@ -80,7 +80,7 @@
color: white;
}

& .yfm-tab {
.yfm-vertical-tab {
border-bottom: unset !important;
}

Expand Down
Loading

0 comments on commit 9b2375f

Please sign in to comment.