   removeTabForItem: (item) ->
     tabIndex = @tabs.findIndex((t) -> t.item is item)
     if tabIndex isnt -1
-      @tabs[tabIndex].destroy()
+      tab = @tabs[tabIndex]
       @tabs.splice(tabIndex, 1)
+      @tabsByElement.delete(tab)
+      tab.destroy()
     tab.updateTitle() for tab in @getTabs()
     @updateTabBarVisibility()
 

BrowserWindow = null # Defer require until actually used
{ipcRenderer} = require 'electron'

{matches, closest, indexOf} = require './html-helpers'
{CompositeDisposable} = require 'atom'
_ = require 'underscore-plus'
TabView = require './tab-view'

    tab.style.maxWidth = '' for tab in @getTabs()
    return

  tabForElement: (element) ->
    currentElement = element
    while currentElement?
      if tab = @tabsByElement.get(currentElement)
        return tab
      else
        currentElement = currentElement.parentElement

module.exports = document.registerElement("atom-tabs", prototype: TabBarView.prototype, extends: "ul")
